php mysql count records

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
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

php mysql count records

Post by tomsace »

Hi,
I have tried to count how many records I have in mysql database.
I am trying to count how many times 'action' is found in the column 'category' in the table 'games'.
I have found a few online demos but still had no luck. How would I edit the following code to count what I mentioned above?

Code: Select all

<?php 
$query = "select * from articles where catID = 4";
$result = mysql_query($query);
 
while ($row = mysql_fetch_array($result))
{
$count = $row["count"];
}
echo $count;
 
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php mysql count records

Post by Benjamin »

Code: Select all

 
SELECT COUNT(*) AS total FROM articles WHERE catID = 4
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php mysql count records

Post by tomsace »

Would I just change it to:

Code: Select all

SELECT COUNT(*) AS total FROM [color=#0000FF]games[/color] WHERE [color=#0000FF]category[/color] = [color=#0000FF]action[/color]
??
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php mysql count records

Post by Benjamin »

I don't know what your database schema looks like, but give it a try and see if it works:)
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php mysql count records

Post by tomsace »

I have tried this but doesnt work.

Table: games
Column: category
I want to count: action

I just don't know what to change on the proper code?!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php mysql count records

Post by Benjamin »

Code: Select all

 
SELECT COUNT(*) AS total FROM games WHERE category = 'action'
 
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php mysql count records

Post by tomsace »

Thats what I have tried!
Why isn't it working? I am getting very frustrated because it seems so easy but still no luck, just blank no errors or anything!!
This is what I have:

Code: Select all

<?php
$query = "SELECT COUNT(*) AS total FROM games WHERE category = 'action'";
$result = mysql_query($query);
 
while ($row = mysql_fetch_array($result))
{
$count = $row["count"];
}
echo $count;
 
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php mysql count records

Post by Benjamin »

Hmm, have you tried turning on error reporting?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: php mysql count records

Post by tomsace »

Hey thanks alot McInfo.
Even though you didnt say anything lol. Just changed the word 'count' to 'total' and it works great!
Post Reply