Knowledge base
1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Use count() rather than mysql_num_rows()
This guide explains why it is recommended to abandon the function mysql_num_rows()
MySQL simply returns the number of rows of a result.
The disadvantage of this function is that it is very heavy for the server since it is a loop that runs 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()
who takes care of this and is much less burdensome. 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: