COUNT how many times "live" is in a column

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

COUNT how many times "live" is in a column

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post by Mark Baker »

Code: Select all

 
SELECT STATUS, 
       COUNT(STATUS) AS num_of 
  FROM usercomments 
 WHERE STATUS = 'live'
 GROUP BY STATUS
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

I think count() is faster...so should probably check out Mark Baker's solution :)
Post Reply