[snip]
Another newbie question but how do I get the info out of a query when I
do a group by? How is it presented to me?

Would you mind taking your example 1 step farther and show me how I
would echo, say, the total of blue eyed people?

$sql = "SELECT COUNT(eyeColor) AS colorCount, eyeColor FROM theTable
WHERE sex = 'male' GROUP BY eyeColor ";

$sql = "SELECT eyeColor, ";
$sql .= "SUM(IF(sex = 'male', 1, 0)) AS Guys, ";
$sql .= "SUM(IF(sex = 'female', 1, 0)) AS Gals ";
$sql .= "FROM theTable ";
$sql .= "GROUP BY eyeColor ";
[/snip]

Let's pretend we are using MySQL (and the AS in the query is very
important) and we are displaying in a web page, I'll show some error
checking etc.;

$sql = "SELECT COUNT(eyeColor) AS colorCount, eyeColor FROM theTable
WHERE sex = 'male' GROUP BY eyeColor ";
/*do the query and check for problems*/
if(!($db = mysql_query($sql, $connectionString))){
        echo "MySQL Error: " . mysql_error() . "\n";
        exit();
}

Assuming no errors I would then do this;

while($result = mysql_fetch_object($db)){
        echo "We have " . $result->colorCount . "guys with " .
$result->eyeColor . "eyes!<br>\n";
}

If you want blue eyes only you would handle that in the query;

SELECT COUNT(eyeColor) FROM theTable WHERE sex = 'male' AND eyeColor =
'blue'

And if your are returning one row ...

$result = mysql_fetch_object($db); // no while loop
echo "We have " . $result->colorCount . "guys with " . $result->eyeColor
. "eyes!<br>\n";

Tim there are several excellent tutorials (Google for PHP tutorial) that
walk you through step by step. 

P.S. when replying to a response on the list make sure to reply to all
so that your message gets returned to the list in the event the original
respondent is not available to reply to your response. 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to