How to Efficiently Count Records of an SQL Table with PHP

Hello,

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.

Richi

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

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 became a Liaison between Arvixe and Clip-Bucket, so I am here to assist you with anything you need regarding Clip-Bucket or any particular computer-related subject you need. Best Regards, Richi

Leave a Reply

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


8 + = 17

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>