search
HomeBackend DevelopmentPHP TutorialThings you should know about PHP+MySQL paging

Things you should know about PHP+MySQL paging

Jun 04, 2018 pm 05:18 PM
phpphp+mysql

What you should know about PHP MySQL paging. This article mainly introduces PHP MySQL paging technology in detail and provides you with complete PHP paging examples. Interested friends can refer to it

As the saying goes, "If you want to do your job well, you must first sharpen your tools." Today we are going to use PHP to implement paging. Then our first task is to build a PHP working environment.

Environment preparation

When using PHP technology, the best partner is AMP (Apache, MySQL, PHP). Now there are many integrated environments, such as WAMP, XAMPP , phpnow and so on. But today I will manually build a PHP working environment.

Apache

We first need to download the Apache server from Apache’s official website. It is best to download the msi version, because then we can configure various environments without having to manually configure it.

Apache download address: an msi version of ApacheServer, the first choice for quickly building a PHP server environment.

MySQL

MySQL, a well-known open source project in the database industry, has now been acquired by Oracle. I don’t know if there will be any charges in the future. But for now, the best choice for PHP development is MySQL. Needless to say, here is the download address.

MySQL download address

During the installation process, remember to remember the user name and password.

PHP

#Some people say that PHP is not a language, but a framework, a client implementation that connects to MySQL. I thought about it carefully, and it seemed to make some sense. But if you put it this way, there are many languages ​​that are not languages ​​at all. As a civilian hero, php's progress is well known. Attached is the download address of php below, so you don’t have to look for it separately.

PHP download address: msi version of PHP, you can quickly build PHP without manually configuring the environment

Working environment

We have installed it After the above three software, you can officially start setting up the environment. For now, all we need to know is that our working directory is under Apache's htdocs folder. htdocs, as a virtual directory, is maintained by the apache configuration file, which we will gradually come into contact with in the future.

Remember that it is the htdocs folder in the apache installation directory.

Database preparation

Although the environment has been set up, we still need to do paging. First of all, there must be data. "Make bricks without straw". Next let's prepare the data.

Create database

Create database statement create database my_database_name;
Here we use MySQL that comes with installation The mysql database is ready. It’s also a little easier.

Building tables

The data warehouse is still built, now we need to "separate rooms", which is where the data is stored ,surface.

create table table_name(···);
Similarly, here we use the built-in database table for laziness. The details are as follows:

mysql> use mysql
Database changed
mysql> desc innodb_table_stats;
+--------------------------+---------------------+------+-----+-------------------+-----------------------------+
| Field   | Type  | Null | Key | Default  | Extra   |
+--------------------------+---------------------+------+-----+-------------------+-----------------------------+
| database_name  | varchar(64)  | NO | PRI | NULL  |    |
| table_name  | varchar(64)  | NO | PRI | NULL  |    |
| last_update  | timestamp  | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| n_rows   | bigint(20) unsigned | NO | | NULL  |    |
| clustered_index_size | bigint(20) unsigned | NO | | NULL  |    |
| sum_of_other_index_sizes | bigint(20) unsigned | NO | | NULL  |    |
+--------------------------+---------------------+------+-----+-------------------+-----------------------------+

Pre-stored data

For the convenience of demonstration, we need to pre-store some data. It doesn't matter whether you use batch import or add manually. The core is still

insert into table_name(···) values(···);

For example, the data we store is as follows:

mysql> select * from innodb_table_stats;
+-----------------+----------------+---------------------+--------+----------------------+--------------------------+
| database_name | table_name | last_update  | n_rows | clustered_index_size | sum_of_other_index_sizes |
+-----------------+----------------+---------------------+--------+----------------------+--------------------------+
| fams  | admin  | 2016-07-19 14:47:02 | 3 |   1 |   0 |
| fams  | assets_in | 2016-07-14 14:42:44 | 2 |   1 |   3 |
| fams  | assets_out | 2016-07-14 20:14:31 | 4 |   1 |   3 |
| fams  | class  | 2016-07-14 14:36:02 | 3 |   1 |   0 |
| fams  | dog  | 2016-08-11 15:25:50 | 4 |   1 |   0 |
| fams  | fixed_assets | 2016-07-14 15:55:09 | 6 |   1 |   2 |
| fams  | sub_class | 2016-07-14 14:38:51 | 8 |   1 |   1 |
| fams  | user  | 2016-07-14 14:15:59 | 2 |   1 |   0 |
| mysql  | gtid_executed | 2016-07-14 12:50:25 | 0 |   1 |   0 |
| privilegesystem | privilege | 2016-08-08 08:56:21 | 3 |   1 |   0 |
| privilegesystem | role  | 2016-08-08 08:26:56 | 2 |   1 |   0 |
| privilegesystem | role_privilege | 2016-08-08 09:51:04 | 2 |   1 |   1 |
| privilegesystem | user  | 2016-08-08 11:07:35 | 2 |   1 |   0 |
| privilegesystem | user_role | 2016-08-08 11:08:15 | 2 |   1 |   2 |
| sys  | sys_config | 2016-07-14 12:50:30 | 6 |   1 |   0 |
| test  | datetest | 2016-07-19 10:02:38 | 2 |   1 |   0 |
+-----------------+----------------+---------------------+--------+----------------------+--------------------------+
16 rows in set (0.00 sec)

PHP expansion preparation

If it is a non-msi version installation, we need to manually enable PHP expansion so that Use some functions of mysql to operate the database.

php.ini

This file is located in the php installation directory. We need to remove the semicolon in front of the code below.

[PHP_MYSQL]
extension=php_mysql.dll
[PHP_MYSQLI]
extension=php_mysqli.dll

In the PHP ini file, The comment is;

Principle of paging

"Everything is ready, all we need is the east wind." Now let’s talk about the core idea of ​​paging.

That is the current page, page size, and total number of records. Through these three, we can calculate the total number of pages through the total number of records and page size. Then implement the corresponding display based on the current page.

Total number of records

// 获取总的记录数
$sql_total_records = "select count(*) from innodb_table_stats";
$total_records_result = mysql_query($sql_total_records);
$total_records = mysql_fetch_row($total_records_result);
echo "总的记录数位: ".$total_records[0]."<br>";

Current page

// 通过GET方式获得客户端访问的页码
$current_page_number = isset($_GET[&#39;page_number&#39;])?$_GET[&#39;page_number&#39;]:1;
if($current_page_number<1) {
 $current_page_number =1;
}
if($current_page_number>$total_pages){
 $current_page_number = $total_pages;
}
echo "要访问的页码为:".$current_page_number;

Paging Core

// 获取到了要访问的页面以及页面大小,下面开始分页
$begin_position = ($current_page_number-1)*$page_size;
$sql = "select * from innodb_table_stats limit $begin_position,$page_size";
$result = mysql_query($sql);

In this way we can get the result set we want. The next thing is how to display it on the page.

Page display

// 处理结果集
echo "<table border=&#39;#CCF solid 1px&#39;><th>Mysql Fixed Assets Table</th>";
echo "<tr><td>DbName</td><td>TableName</td><td>Last_update</td><td>n_Nows</td><td>Clustered_Index_Size</td><td>Sum_od_Other_Index_sizes</td></tr>";
while(($row = mysql_fetch_row($result))){
 echo "<tr>";
 echo "<td>".$row[0]."</td>";
 echo "<td>".$row[1]."</td>";
 echo "<td>".$row[2]."</td>";
 echo "<td>".$row[3]."</td>";
 echo "<td>".$row[4]."</td>";
 echo "<td>".$row[5]."</td>";
 echo "</tr>";
}
echo "</table>";

// 循环显示总页数
?>
<?php
echo &#39;<a href="SlicePage.php?page_number=1">首页</a>  &#39;;
for($i=1;$i<=$total_pages;$i++){
 echo &#39;<a href="./SlicePage.php?page_number=&#39;.$i.&#39;">第&#39;.$i.&#39;页</a>  &#39;; 
}
echo &#39;<a href="SlicePage.php?page_number=&#39;.($current_page_number-1).&#39;">上一页</a>  &#39;;
echo &#39;<a href="SlicePage.php?page_number=&#39;.($current_page_number+1).&#39;">下一页</a>  &#39;;
echo &#39;<a href="SlicePage.php?page_number=&#39;.($total_pages).&#39;">尾页</a>  &#39;;

Paging implementation

After understanding the above content, let’s take a look at this complete example. Bar.

Code SlicePage.php


The resulting initial page is:

Things you should know about PHP+MySQL paging

Click on the page number

Things you should know about PHP+MySQL paging

Next page

Things you should know about PHP+MySQL paging

Summary

Pagination is a very Practical technology, compared to Java implementation, PHP is still very flexible to implement. It frees people from cumbersome object-oriented programming and can indeed give people a sense of beauty when their ideas are clear.

The above is the detailed content of Things you should know about PHP+MySQL paging. For more information, please follow other related articles on the PHP Chinese website!

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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

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 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)