PHP paging efficiency final version (recommended)

Source: Internet
Author: User
This article provides a detailed analysis of PHP paging implementation. For more information, see Here we only show the design ideas, specific security issues, whether to synchronously update, etc. you can add and modify as needed:
In the past, the commonly used PHP paging technology usually requires statistics on the total number of information items in the database to determine the total number of pages and paging them, that is, the database needs to be called twice on each page. in the face of large data volumes and high concurrent queries, it is very inefficient. I have been worried about this problem but I did not think of a proper solution, today, I suddenly encountered a miracle in my sleep... the specific analysis principle is as follows: (the red part is the difference after optimization, and the idea is to use the original code for writing, in order to take care of new people)
Original paging technology: including technologies that are also used by many open-source programs;
Generally, statistics are performed on the database information, and the paging class is called for paging. Two database queries are performed each time.
Example (original paging technology): perform two database queries each time
The code is as follows:
$ Sqlstr = "select count (*) as total from tablename ";
$ SQL = mysql_query ($ SQL) or die ("error ");
$ Info = mysql_fetch_array ($ SQL); // The first database call
$ Total = $ info ["total"]; // you must query the total number of information records for each page turning.
$ Pagesize = 10; // The number displayed on each page
$ Page = $ _ GET ["page"]? Max (intval ($ _ GET ["page"]), 1): 1; // current page
If ($ total ){,
$ SQL = "select * from tablename limit" ($ page-1) * $ pagesize ", $ pagesize ";
$ SQL = mysql_query ($ SQL) or die ("error"); // The second database query operation
$ Info = mysql_fetch_array ($ SQL );
Do {
...............
} While ($ info = mysql_fetch_array ($ SQL ));
Include ("page_class.php"); // call the paging class
$ Url = "url. php? Page = "// assume that the current page is URL. PHP
Echo $ get_page = new get_page ($ url, $ total, $ pagesize, $ page); // The URL is the URL address to be paged.
}
// Optimized paging technology (you only need to make statistics when calling the page for the first time)
If (isset ($ _ GET ["total"]) {// you only need to calculate the total number of information items at a time.
$ Total = intval ($ _ GET ["total"]);
// The total number of subsequent information can be passed through GET, saving 1/2 of the database load ,,,,
} Else {
$ Sqlstr = "select count (*) as total from tablename ";
$ SQL = mysql_query ($ SQL) or die ("error ");
$ Info = mysql_fetch_array ($ SQL); // The first database call
$ Total = $ info ["total"];
} // Total number of information items
$ Pagesize = 10; // The number displayed on each page
$ Page = $ _ GET ["page"]? Max (intval ($ _ GET ["page"]), 1): 1; // current page
If ($ total ){
$ SQL = "select * from tablename limit" ($ page-1) * $ pagesize ", $ pagesize ";
$ SQL = mysql_query ($ SQL) or die ("error"); // The second database query operation
$ Info = mysql_fetch_array ($ SQL );
Do {
...............
} While ($ info = mysql_fetch_array ($ SQL ));
Include ("page_class.php"); // call the paging class
$ Url = "url. php? Total = $ total & page = "// assume that the current page is URL. PHP
Echo $ get_page = new get_page ($ url, $ total, $ pagesize, $ page); // The URL is the URL address to be paged.
}

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.