How do I count rows with MySQL?

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
xenoalien
Forum Newbie
Posts: 19
Joined: Fri Apr 04, 2008 12:11 pm
Location: Cyberspace

How do I count rows with MySQL?

Post by xenoalien »

How do I count rows with MySQL? I am making a wallpaper website for fun and kicks located at http://www.wallpapersearch.net and would like to know how I could count all the results of a database table? Basically this is so that I can show visitors how much wallpaper there is within my database. Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I count rows with MySQL?

Post by Christopher »

SELECT COUNT(*) AS total FROM wallpapers
(#10850)
robseek
Forum Newbie
Posts: 7
Joined: Fri Apr 11, 2008 10:19 am

Re: How do I count rows with MySQL?

Post by robseek »

If you want to find out how many rows have been returned from a particular sql query then you would do this:

Code: Select all

 
$sql = "SELECT color FROM wallpaper WHERE id = $input_id;";
$result = mysql_query($sql);
$num_of_rows = mysql_num_rows($result);
 
Post Reply