SQL_CALC_FOUND_ROWS vs mysql_num_rows()
What function do you use to return the number of records in a MYSQL table and why?
To understand the difference between SQL_CALC_FOUND_ROWS and mysql_num_rows()

$sql = mysql_query("
SELECT SQL_CALC_FOUND_ROWS id
FROM table
WHERE id_msg = 2
");
$pages_resultats = mysql_query("SELECT FOUND_ROWS()");
list($nbr_resultats) = mysql_fetch_array($pages_resultats);
Resultats :
echo mysql_num_rows($sql); // Return 4 echo $nbr_resultats; // Return 4
Now add “Limit” to query and retry :
$sql = mysql_query("
SELECT SQL_CALC_FOUND_ROWS id
FROM table
WHERE id_msg = 2
LIMIT 2
");
$pages_resultats = mysql_query("SELECT FOUND_ROWS()");
list($nbr_resultats) = mysql_fetch_array($pages_resultats);
Resultats :
echo mysql_num_rows($sql); // Return 2 echo $nbr_resultats; // Return always 4
Unlike mysql_num_rows() or COUNT(), the function SQL_CALC_FOUND_ROWS is not affected by the LIMIT clause.
SQL_CALC_FOUND_ROWS is ideal for paging navigation.
September 26th, 2009 in
Development, Tricks | tags: mysql_num_rows(), SQL_CALC_FOUND_ROWS |
1 Comment