1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Use count() instead of mysql_num_rows()
This guide explains why it is recommended to abandon the use of the mysql_num_rows()
function in MySQL, which simply returns the number of rows in a result.
The disadvantage of this function is that it is very heavy for the server since it is a loop that iterates through each row to count them.
For example:
$SQLstr = "SELECT * FROM comments WHERE display=1";
$r = mysql_query($SQLstr);
$num = mysql_num_rows($r);
MySQL has a count()
function that handles this and is much less heavy. Use this:
$SQLstr = "SELECT count(*) FROM comments WHERE display=1"
$r = mysql_query($SQLstr);
$result = mysql_fetch_row($r);
$num = $result[0];
or
$SQLstr = "SELECT count(*) as total FROM comments WHERE display=1"
$r = mysql_query($SQLstr);
$result = mysql_fetch_array($r);
$num = $result['total'];