Home Backend Development PHP Tutorial Things you should know about PHP+MySQL paging

Things you should know about PHP+MySQL paging

Jun 04, 2018 pm 05:18 PM
php php+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  |    |
+--------------------------+---------------------+------+-----+-------------------+-----------------------------+
Copy after login

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)
Copy after login

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>";
Copy after login

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;
Copy after login

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);
Copy after login

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;;
Copy after login

Paging implementation

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

Code SlicePage.php


Copy after login

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 of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles