Re: records in db

From: Date: Sun, 11 Aug 2002 02:09:11 +0000
Subject: Re: records in db
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-111995@lists.php.net to get a copy of this message
on 11/08/02 3:52 AM, Tyler Durdin (linuxgeek23@hotmail.com) 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

« previous php.general (#111995) next »