Home  >  Article  >  Backend Development  >  PHP paging efficiency final version (recommended)_PHP tutorial

PHP paging efficiency final version (recommended)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:01:45766browse

Only the design ideas, specific security issues, whether to synchronize updates, etc. are displayed here. Modifications can be added according to specific needs:
In the past, people commonly used PHP paging technology, which usually required counting the total information items in the database. Only by counting can we determine the total number of pages and paginate them. In other words, each paging requires two calls to the database, which is very inefficient when faced with large amounts of data and high concurrent queries. I have always been worried about this. The problem is bothering me but I haven’t thought of a suitable 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, the idea is written in the most original code, in order to take care of newcomers)
Original paging technology: including technology also used by many open source programs;
Generally, database information is counted, and then the paging class is called to perform paging, every time It needs to go through 2 database query operations
Example (original paging technology): 2 database queries are required each time

Copy code 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"];//Every time the page is turned, a database query operation of the total number of pieces of information is required
$pagesize=10; //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)); Assume that the current page is URL.PHP

echo $get_page=new get_page($url,$total,$pagesize,$page); //URL is the URL address to be paginated }
/ /Optimized paging technology (only need to perform information statistics on the first call)

if(isset($_GET["total"])){ //Only need to perform total information once The statistics of the number can be
 $total=intval($_GET["total"]);
//The total number of information in the future 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
                                                                                                                    through out using using  through   through   through   through   through   through ‐ to ’s ’s ’s out off-to-under-downloaded, etc. mysql_fetch_array($sql);                                                    Number displayed per 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");//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); //URL is the URL address to be paginated
}





http://www.bkjia.com/PHPjc/327980.html
www.bkjia.com

true

TechArticleHere only shows the design ideas, specific security issues, whether to synchronize updates, etc. can be added and modified according to specific needs: People in the past Commonly used PHP paging technology usually requires statistics of data first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn