Page 1 of 1
php mysql count records
Posted: Wed Apr 15, 2009 6:33 pm
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;
?>
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:37 pm
by Benjamin
Code: Select all
SELECT COUNT(*) AS total FROM articles WHERE catID = 4
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:42 pm
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]
??
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:43 pm
by Benjamin
I don't know what your database schema looks like, but give it a try and see if it works:)
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:45 pm
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?!
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:50 pm
by Benjamin
Code: Select all
SELECT COUNT(*) AS total FROM games WHERE category = 'action'
Re: php mysql count records
Posted: Wed Apr 15, 2009 6:55 pm
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;
?>
Re: php mysql count records
Posted: Wed Apr 15, 2009 7:05 pm
by Benjamin
Hmm, have you tried turning on error reporting?
Re: php mysql count records
Posted: Thu Apr 16, 2009 11:04 am
by tomsace
Hey thanks alot McInfo.
Even though you didnt say anything lol. Just changed the word 'count' to 'total' and it works great!