For some database queries, you not only want to get the query results, but also want to conveniently count the number of records in the query results. My usual practice is:
$ Q = "select * from $ fromTable where $ where limit $ start, $ pageSize ";
$ R = mysql_query ($ q );
$ Q = "select count (*) from $ fromTable where $ where ";
$ Cnt = mysql_query ($ q );
When
However, you can use mysql_num_rows () or mysql_affected_rows () to obtain the number of records after the first query, but both functions return a query.
The number of results is affected by the limit statement. In many cases, you need to know the total number of results of this query statement without limit, such as paging query.
Mysql
In itself supports a better way to achieve the above effect, that is, using SQL _CALC_FOUND_ROWS and FOUND_ROWS () functions. See http:
// Dev.mysql.com/doc/refman/5.0/en/information-functions.html?function_found-rows
Example:
Mysql>SELECT SQL _CALC_FOUND_ROWS * FROMTbl_name
->WHERE id & gt; 100 LIMIT 10;
Mysql>SELECT FOUND_ROWS ();