PHP paging efficiency final version (recommended ). Here we only show the design ideas, specific security issues, whether to synchronously update, etc. you can add and modify as needed: the PHP paging technology that people used in the past usually needs to collect statistics first.
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.
}
In the past, the paging technology of PHP is commonly used. statistics are usually collected first...