// untested

$data = mysql_fetch_array(mysql_query("SELECT count(answer_col) as total
FROM answers_table WHERE answer_col='{$answer}'"));

echo $data[total];

Put this in a while/foreach statement

Regards
        Chris Kay

> -----Original Message-----
> From: Justin French [mailto:[EMAIL PROTECTED]] 
> Sent: Sunday, 11 August 2002 12:09 PM
> To: Tyler Durdin; [EMAIL PROTECTED]
> Subject: Re: [PHP] records in db
> 
> 
> on 11/08/02 3:52 AM, Tyler Durdin ([EMAIL PROTECTED]) wrote:
> 
> > If I have a field in my DB that can have 4 different 
> answers, lets say 
> > a,b,c and d. How can I count the number of each in that 
> field. So if 
> > there are 4 a's 1 b 0 c's and 12 d's how can I get php to 
> count this?
> 
> I'm pretty certain there's a way to do this with just one 
> MySQL query, but here's a PHP version that does 4 queries:
> 
> <? // UNTESTED CODE
> $answers = array('a','b','c','d');
> foreach($answers as $key => $answer)
>     {
>     $sql = "SELECT * FROM answers_table WHERE answer_col='{$answer}'";
>     $result = mysql_query($sql);
>     if($result)
>         {
>         $count = mysql_num_rows($result);
>         }
>     else
>         {
>         $count = "0";
>         }
>     echo "{$count} people selected answer {$answer}<br />";
>     }
> ?>
> 
> Should print out something like:
> 
> 4 people selected answer a
> 1 people selected answer b
> 0 people selected answer c
> 12 people selected answer d
> 
> 
> hack it to suit your needs,
> 
> Justin
>         
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


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

Reply via email to