Home  >  Article  >  Database  >  PHP development practice: How to use PHP and MySQL to implement paging function

PHP development practice: How to use PHP and MySQL to implement paging function

WBOY
WBOYOriginal
2023-07-02 19:43:401381browse

PHP development practice: How to use PHP and MySQL to implement paging function

In Web development, paging function is a very common and necessary function. In the case of large amounts of data, it is not feasible to display all the data to users at once, so paging processing is usually required to divide the data into several pages for display. This article will introduce how to use PHP and MySQL to implement paging function.

First, we need to create a database and create a table in the database to store the data that needs to be displayed in pages. Suppose we have a student table that contains the fields id, name, and age, as shown below:

CREATE TABLE students (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50),
  age INT
);

Next, we need to write PHP code to connect to the database and obtain the data that needs to be displayed. We can use PHP's MySQLi extension for database operations. The specific code is as follows:

<?php
$host = "localhost"; // 数据库主机
$username = "root"; // 数据库用户名
$password = "password"; // 数据库密码
$dbname = "mydb"; // 数据库名

// 创建数据库连接
$conn = new mysqli($host, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

// 定义每页显示的记录数
$pageSize = 10;

// 获取当前页码
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 计算查询的起始位置
$start = ($page - 1) * $pageSize;

// 查询需要展示的数据
$sql = "SELECT * FROM students LIMIT {$start}, {$pageSize}";
$result = $conn->query($sql);

// 输出数据
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    echo "ID: " . $row["id"]. " - 姓名: " . $row["name"]. " - 年龄: " . $row["age"]. "<br>";
  }
} else {
  echo "没有数据";
}

// 计算总页数
$sql = "SELECT COUNT(*) AS total FROM students";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$totalPages = ceil($row['total'] / $pageSize);

// 输出分页链接
for ($i = 1; $i <= $totalPages; $i++) {
  echo "<a href='?page={$i}'>{$i}</a> ";
}

// 关闭数据库连接
$conn->close();
?>

The above code first connects to the database and obtains the current page number and the number of records displayed on each page. Then use the LIMIT clause to query the data that needs to be displayed based on the starting position of the calculation, and output the data to the page. Then calculate the total number of pages and output the corresponding paging links based on the total number of pages. Finally close the database connection.

You can see the paging effect by visiting http://localhost/page.php. By clicking on the paging link, you can switch to different page numbers to view the corresponding data.

Summary:

This article introduces how to use PHP and MySQL to implement paging function. By connecting to the database, and by calculating the starting position and using the LIMIT clause to perform paging queries, the paging display function of data is realized. I hope this article will be helpful for beginners to understand and master the implementation of paging function.

The above is the detailed content of PHP development practice: How to use PHP and MySQL to implement paging function. 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