[RESOLVED] Counting the number of Rows... efficiently
Currently I am using an MySQL query to gather all the IDs pertaining to a specific category and then I use mysql_num_row() to count the number of rows to get the total.
Is there a more efficient way to do this? I know there is a COUNT() for MySQL but I didn't know how to return that and actually use it in PHP.
Thanks
Re: Counting the number of Rows... efficiently
I believe it is
PHP Code:
$count = mysql_result(mysql_query('SELECT COUNT(*) FROM `table_name`'), 0);
As for efficiency, not sure. Loop each one a few hundred times and benchmark them using microtime() if you're concerned :)
Edit: My educated guess is that if you require the table data anyway then mysql_num_rows() will be more efficient than two queries. But if all you need is a row count then this will be more efficient.
Re: Counting the number of Rows... efficiently