Page 1 of 1

COUNT how many times "live" is in a column

Posted: Thu Sep 17, 2009 5:43 am
by simonmlewis
Hi

I have a simple query but cannot recall how to do this.

Code: Select all

 
include "dbconn.php";
$result = mysql_query ("SELECT status, COUNT(status) as num_of FROM usercomments WHERE status = 'live'");
while ($row = mysql_fetch_array($result)){
echo "Live comments" .$row['num_of'];   
}
mysql_free_result($result);
I need to count how many "live" there are in a table. ie. if 50 people have sent in something, and we have made 20 'live', STATUS has "live" entered into it. I need to see how many "live" there are.

RESOLVED Re: COUNT how many times "live" is in a column

Posted: Thu Sep 17, 2009 5:49 am
by simonmlewis
Problem solved, this worked instead:

Code: Select all

$result = mysql_query ("SELECT status FROM usercomments WHERE status = 'live'");
$num_rows = mysql_num_rows($result); 
echo "Total live: $num_rows \n"; 
mysql_free_result($result);

Re: COUNT how many times "live" is in a column

Posted: Thu Sep 17, 2009 6:02 am
by Mark Baker

Code: Select all

 
SELECT STATUS, 
       COUNT(STATUS) AS num_of 
  FROM usercomments 
 WHERE STATUS = 'live'
 GROUP BY STATUS
 

Re: COUNT how many times "live" is in a column

Posted: Thu Sep 17, 2009 7:03 am
by jackpf
I think count() is faster...so should probably check out Mark Baker's solution :)