


Detailed explanation of paging and SqlHelper class usage examples in php
This article mainly introduces the usage of paging and SqlHelper classes in PHP, and analyzes the definitions and specific usage techniques of PHP database query classes and paging classes in the form of examples. Friends in need can refer to it
The details are as follows :
The document directory structure is as follows:
The SqlHelper.php code is as follows:
<?php /** * Created by JetBrains PhpStorm. * User: lee * Date: 13-7-26 * Time: 下午8:30 * To change this template use File | Settings | File Templates. */ class SqlHelper{ private $mysqli; private static $host="localhost"; private static $user="root"; private static $pwd=""; private static $db="world"; private $sql=false; private $result=false; function __construct(){ $this->mysqli=new MySQLi(self::$host,self::$user,self::$pwd,self::$db); if($this->mysqli->connect_error){ die("连接数据库失败! ".$this->mysql->connect_error); } $this->mysqli->query("set names utf8"); } function execute_dql_all($sql){ //执行查询语句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //将数据转存到$arr数组中 while($row=mysqli_fetch_array($this->result,MYSQL_BOTH)){ $arr[]=$row; } $this->result->free(); return $arr; } function execute_dql_num($sql){ //执行查询语句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //将数据转存到$arr数组中 while($row=mysqli_fetch_array($this->result,MYSQLI_NUM)){ $arr[]=$row; } $this->result->free(); return $arr; } function execute_dql_assoc($sql){ //执行查询语句 $arr=array(); $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error); //将数据转存到$arr数组中 while($row=mysqli_fetch_array($this->result,MYSQLI_ASSOC)){ $arr[]=$row; } $this->result->free(); return $arr; } //查询某表中的记录数 function execute_dql_counts($table,$id="*"){ $this->sql="select count($id) from $table"; $this->result=$this->mysqli->query($this->sql); $row=mysqli_fetch_all($this->result); $this->result->free(); return $row[0][0]; } function execute_dml($sql){ //执行正删改 $this->result=$this->mysqli->query($sql); if(!$this->result){ return -1;//执行正删改失败 }else{ if($this->mysqli->affected_rows>0){ return 1;//执行正删改成功,影响行数 }else{ return 0;//执行正删改成功,但没有影响行数 } } } }
Paging.php code is as follows:
<?php /** * Created by JetBrains PhpStorm. * User: lee * Date: 13-7-27 * Time: 下午2:48 * To change this template use File | Settings | File Templates. */ header("Content-type:text/html;charset=utf-8;"); require_once("SqlHelper.php"); class Paging { private $sqlHelper=false; private $pageCount=false;//页数 private $counts=false;//总记录数 private $returnArr=false;//分页超链接的分页 function __construct(){ $this->sqlHelper=new SqlHelper(); $this->returnArr=array(); } /* * 参数说明 * * $table 分页时对那个表的数据分页 * $id 辅助查询当前分页的数据表的总记录数 * $pageSize 每页显示多少条信息记录数 * $pagingSize 分页栏每次循环显示出来的个数 * $nowPage 当前是第几页,默认第一页 * $href 分页栏的超链接将要往哪里连接 */ function paging_prev_next($table,$id="*",$pageSize,$pagingSize,$nowPage=1,$href){ $this->counts=$this->sqlHelper->execute_dql_counts($table,$id); $this->pageCount=ceil($this->counts/$pageSize); $this->returnArr["count"]=$this->counts; $this->returnArr["start"]=($nowPage-1)*$pageSize; $this->returnArr["limit"]=$pageSize; if($nowPage>$this->pageCount || $nowPage<=0){ return false; } $t=(ceil($nowPage/$pagingSize)-1)*$pagingSize+1; $pre=$nowPage-$pagingSize; $nex=$nowPage+$pagingSize; echo " <span class='paging-list-a paging-list-a-withBg'>{$nowPage}/{$this->pageCount}</span> <a href='{$href}?nowPage={$pre}' class='paging-list-a'><</a>"; for($i=$t;$i<$t+$pagingSize;$i++){ if($i*$pageSize>$this->pageCount*$pageSize){ break; }else{ if($nowPage==$i){ echo " <a href='{$href}?nowPage={$i}' class='paging-list-a paging-list-a-withBg'>{$i}</a>"; }else{ echo " <a href='{$href}?nowPage={$i}' class='paging-list-a'>{$i}</a>"; } } } echo " <a href='{$href}?nowPage={$nex}' class='paging-list-a'>></a>"; return $this->returnArr; } }
paging-list-link.css code is as follows:
/** * Created by JetBrains PhpStorm. * User: lee * Date: 13-7-27 * Time: 下午5:56 * To change this template use File | Settings | File Templates. */ .paging-list-a{ border:1px solid #b5b5af; background-color:#efebed; font-family: 'Meiryo UI'; font-size: 16px; font-weight: 600; padding: 0px 8px 0px 8px; /*cursor: pointer;*/ text-decoration: none; color: #292927; } .paging-list-a-withBg{ background-color: #1D92E2; color: white; }
usePaging.php code is as follows:
<!DOCTYPE html> <html> <head> <title></title> <link rel="stylesheet" type="text/css" href="paging-list-link.css"> </head> <body> <?php header("Content-type:text/html;charset=utf-8;"); require_once 'Paging.php'; $paging=new Paging(); //参数说明 /* * $table 分页时对那个表的数据分页 * $id 辅助查询当前分页的数据表的总记录数 * $pageSize 每页显示多少条信息记录数 * $pagingSize 分页栏每次循环显示出来的个数 * $nowPage 当前是第几页,默认第一页 * $href 分页栏的超链接将要往哪里连接,当前页链接地址 */ //控制起始页为 $nowPage=1; if(isset($_GET["nowPage"])){ $nowPage=$_GET["nowPage"]; } //定义分页所需参数 $meiyexiansi=10; $meiyelianjieshu=10; $receiveArr=array(); $receiveArr=$paging->paging_prev_next("city","ID",$meiyexiansi,$meiyelianjieshu,$nowPage,"usePaging.php"); //容错判断 if(!$receiveArr){ return; } //查询每页需要显示的数据,大小限制存在 $receiveArr 数组中 $sqlHelper=new SqlHelper(); $result=$sqlHelper->execute_dql_num("select * from city limit ".$receiveArr['start'].",".$receiveArr['limit'].""); echo "<pre class="brush:php;toolbar:false">"; print_r($result); echo ""; ?>
The above is the detailed content of Detailed explanation of paging and SqlHelper class usage examples in php. For more information, please follow other related articles on the PHP Chinese website!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
