1

As I know a pagination structure requires minimum two sql query.

  • First, find total row

  • Second, limit your query.

Is there a way to decrease query to one. Can we use first sql query to manupulate the pagination? On first query we already fetch all necessary data. Is first query's array can handle this issue?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
hakki
  • 6,181
  • 6
  • 62
  • 106
  • 1
    Why would you need two queries for pagination? Can you give an example? Because I can't figure out why two are needed. For the count you can use SQL_CALC_ROWS_FOUND (yes, it does use a second query, but it is much less work than two select queries as you are just getting a variable value on the second query). See: http://stackoverflow.com/questions/5928611/find-total-number-of-results-in-mysql-query-with-offsetlimit – Jonathan Kuhn Apr 20 '15 at 20:15

1 Answers1

3

You can do a simple previous/next pagination with a single query.
For this i your result limit is say 25, just query for 26 and only display the 25. If you get back less than 26 results, you know you don't have any more.

However if you are wanting to accurately display links for page 1,2,3,etc.. you have to do both a query for the total number of records in the table, and a query for just the data you want to display.

cwurtz
  • 3,177
  • 1
  • 15
  • 15