How to Efficiently Count Records of an SQL Table with PHP


For the last website I made, I created a really flexible and friendly administration panel where there were a lot of statistics to analyze how the website was performing. I came across different ways of counting records in a table, yet stuck with this one, the most efficient:

The query is really simple, we will use SELECT COUNT(1), then mysql_fetch_row() that count. The code would look like this:

$q="SELECT COUNT(1) FROM table";
$r = mysql_query($q);
$row = mysql_fetch_row($r);
//The count is stored in the array key 0: $row[0]
print "Has a total of".$row[0]." records";

That’s it! Easy, huh? You can Google how to count records, and see this is the most recommended way of doing it. StackOverflow has a lot of really nice info about this. If you have any question, please do not hesitate to contact me!

Best Regards.


Owner of Juapo2Services

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under Programming/Coding | RSS 2.0

Author Spotlight

Richi González

I started programming when I was 12. I'm a Developer and have been working with the Clip-Bucket script since it came out. I'm the Liaison between Arvixe and Clip-Bucket, so anything you need regarding Clip-Bucket or any particular computer-related subject you need, let me know so I can assist you.

Leave a Reply

Your email address will not be published. Required fields are marked *