Knowledge base
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 MySQL function mysql_num_rows()
which simply returns the number of lines in a result.
The disadvantage of this function is that it is very heavy for the server since it is a loop that goes through each line to count them.
For example:
$SQLstr = "SELECT * FROM commentaires WHERE affiche=1";
$r = mysql_query($SQLstr);
$num = mysql_num_rows($r);
MySQL has a function count()
that handles this and is much less resource-intensive. Use this:
$SQLstr = "SELECT count(*) FROM commentaires WHERE affiche=1"
$r = mysql_query($SQLstr);
$result = mysql_fetch_row($r);
$num = $result[0];
or
$SQLstr = "SELECT count(*) as total FROM commentaires WHERE affiche=1"
$r = mysql_query($SQLstr);
$result = mysql_fetch_array($r);
$num = $result['total'];
Link to this FAQ: