For a long time, whenever I wanted to do paging to browse through a table, I used to run 2 queries. The first would get the results, and the second would be an almost identical query, with a count() instead of fields, and I’d use the result of the second query to figure out how many pages I would need.
Now there’s a better solution, and it’s called found_rows(). Say you have a simple select statement:
select * from sometable LIMIT 10;
You don’t know how many rows are in the table, so you need to do a second query:
select count(1) from sometable;
Now you can calculate the number of pages.
Using found_rows, you write your first query as:
select SQL_CALC_FOUND_ROWS * from sometable LIMIT 10;
select found_rows();
Pagination with MySQL couldn’t be easier.
While this won’t …
[Read more]