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
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment