Re: [Newbie] Simple stats from mysql table data

From: Date: Tue, 18 May 2004 17:37:11 +0000
Subject: Re: [Newbie] Simple stats from mysql table data
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-186495@lists.php.net to get a copy of this message
"Vans Hallden" <php@sijoittaja.net> wrote in message news:p0610050ebccf9e55db6b@[194.86.83.84]... > >Here is one way: > >(probably not the best) > > I was able to get almost everything working with your help. Thanks!!! :) > > >>average ages for male/female respondents (database column 'age') > > > >SELECT AVG(age) FROM tablename WHERE sex = 'male'; > >SELECT AVG(age) FROM tablename WHERE sex = 'female'; > > I have one small problem remaining. With this code: > > $male_average_age = mysql_query("SELECT AVG(age) FROM my_database > WHERE sex = 'M'"); > > ..I get a strange "Resource id#10" output. I can't figure out why. > What am I missing? mysql_query() is returning a result resource handle. You have to use mysql_fetch_row or a similar function to access the result set: $result = mysql_query("SELECT AVG(age) as avgAge FROM my_database WHERE sex = 'M'"); // get the first row of the result as an associative array $row = mysql_fetch_assoc($result); $male_average_age = $row['avgAge']; Take a look here: http://de2.php.net/manual/en/function.mysql-fetch-assoc.php Regards, Torsten > -- > > -- > Hans Vallden > hans@vallden.com

« previous php.general (#186495) next »