mySQL database query LIMIT help... How to create pages if number of rows returned exceeds 'x'?
You can use LIMIT x, y
to show the y
results after x
.
Let's say you have a variable $page
that is passed through $_GET
or through some other means. $page
defaults to 1, or is a numerical value that denotes the page number.
You can then select the rows of that page via:
SELECT * FROM `table` WHERE `condition` = ? LIMIT ($page - 1) * 10, 10
You can replace 10 with how many results per page. As you can see, if the page number is 1, this query is:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 0, 10
Which displays the first 10 results. If the page number is 2, this query is:
SELECT * FROM `table` WHERE `condition` = ? LIMIT 10, 10
Which displays the ten rows after the 10th row, i.e., rows 11-20 (which should be on page 2!)