Discussion on paging processing of php + mysql

Source: Internet
Author: User

Php + mysql paging processing: 1. Use select count (*) from tbl_name get the total number of records to be paged 2. calculate the total number of pages based on the number of records on each page: Total number of pages = ceil (total number of records/number of records on each page) 3. Calculate the start position based on the current page number: Start position = (current page number-1) * Number of records per page 4. select * from tbl_name limit start position, obtain the records to be displayed on each page. 5. List output information in this process, the database needs to traverse the table twice to obtain the required data. Although limit will terminate the traversal after obtaining the specified number of records, it is a waste of searching until the "starting position. Here we propose a new algorithm to discuss with you: 1. Use mysql user variables to split and extract the ID number at the beginning of each page. 2. The number of records in the query result is the total number of pages. 3. Obtain the start id of the current page based on the current page number. 4. Use select * from tbl_name where id> = start id limit to specify the number of records on each page. obtain the record to be displayed. 5. view the list output information, in the next query. Because id is used as the primary key feature, the database can directly locate the required records. This reduces the query time. This query algorithm has a by-product: it can generate a duplicate record for linking up and down pages, that is, there is an overlapping record between each page. Of course, it is easy to remove him. The following is the test code: = ";} If ($ mode) $ pagesize ++; mysql_query (" set @ v: =-1 "); // define the mysql user variable $ rs = mysql_query ("select @ v: = (@ v + 1) as xh, id from data HAVING mod (xh, $ pagesize) = 0 order by id $ order "); mysql_result_all ($ rs); // check the distribution of each page echo $ pages = mysql_num_rows ($ rs ); // obtain the total number of pages if ($ mode) $ pagesize --; // test the paging result. $ I indicates that the page is displayed for ($ I = 0; $ I <$ pages; $ I ++) {mysql_data_seek ($ rs, $ I); // move the result set pointer list ($ xh, $ id) = mysql_fetch_row ($ rs ); // obtain the start id echo"


[$ I] $ xh -- $ id "; $ rs1 = mysql_query (" select * from data where id $ expr $ id order by id $ order limit $ pagesize "); mysql_result_all ($ rs1); // display related results}?> The mysql_result_all.inc function has been pasted many times and is very useful for debugging programs. ". Mysql_field_name ($ result, $ I )."";}Echo" "; while ($ row = mysql_fetch_row ($ result) {echo" "; for ($ I = 0; $ I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.