Pagination on MySQL leads to PHP

I never considered this (pagination) as a problem until recently. When I sat down and shoved him, I found myself facing a lot of problems.

What I need is a basic contact management system in which the user can add / update / delete / search for contacts. Part of the search is where I need pagination to be implemented efficiently.

What I mean (with + ve and -ve points)

  • I can indicate pageNoand offset, and POSTgo to my page search.php. This page will run a simple MySQL query to get the results. Since the number of rows can pretty much work in the thousands, I need to paginate it. Pretty simple, but I need to run the same query over and over for every other page. Meaning, when the user goes from page 1 to page 2, the same MySQL query will be launched (of course, with a different one offset), which, in my opinion, is redundant, and I try to avoid it.
  • Then I thought about capturing the whole result set and storing it in $_SESSION, but in this case, what if the results are simply huge? Will this affect performance in any way?
  • On similar lines, such as the second paragraph, I thought about writing the results to a file, which is plain crap! (I just put it here as a point. I know this is a REAL bad way to do something.)

My questions:

A . Which of the above methods do I implement? Which one is better? Are there any other methods? I have googled, but I believe most examples follow paragraph 1 above.

In . My questions for point1: How can we rely on the order of mysql results? Suppose that the user goes to page2 after a while, how can we be sure that the records of the first tenant are repeated the second time? (Because we are making a new request).

. MySQL? , a mysql_query(..) a resource. , PHP ? ( $_SESSION).

, !: -)

PS: , . , .

+3
4

. , . "" . , . , (memcache), , .

MySQL . , 1 . 2, - ​​ 1 2. : .

- MySQL - , . ( , ..). .

+4

. , . , , , , , , , , -.
, .

. , . .

. PHP . . pagination.

+1

SQL - (0 - , 10 - )

SELECT * FROM `your_table` LIMIT 0, 10 

10 .

, 3 , 30 1-10,11-20,21-30.

SELECT * FROM `your_table` LIMIT 10 OFFSET 0
SELECT * FROM `your_table` LIMIT 10 OFFSET 10
SELECT * FROM `your_table` LIMIT 10 OFFSET 20

Edit:

, , 1 - . . - , $offset = ($pageNum - 1 ) * 10;.

ORDER BY. , , . , .

mysql_query() . , . script, mysql_close() .

+1

( ), , , , mysql. , order by last_updated_time, .

, , . , , - , , ( , mysql).

, , ( : LOL), .

+1

Source: https://habr.com/ru/post/1793359/


All Articles