Pagination when selecting data from database

Have a look at the LIMIT keyword in your query, that can specify the start and count for the records it returns. So I’d do a count first to calculate how many pages to show, then pass the starting record through on the “next page” link so you know where to start the next query.

You could pass start and count values via the link and leave your query as it is, just use the start value to decide how many results to ignore at the start of the loop. But that’s inefficient, especially if you’re returning a lot of data on the query.