How to Efficiently Count Records of an SQL Table with PHP
Written by Richi González Friday, 4 January 2013
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 print "Has a total of".$row." 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!
Owner of Juapo2Services
Looking for quality web hosting? Look no further than Arvixe Web Hosting!