Home Backend Development PHP Tutorial php+mysql分页处理的探讨_PHP

php+mysql分页处理的探讨_PHP

Jun 01, 2016 pm 12:40 PM
echo id ord deal with Discuss Record start

php+mysql分页处理的探讨
常见的分页处理流程为:
1、用select count(*) from tbl_name取得待分页的总记录数
2、根据每页的记录数计算出总页数:总页数 = ceil(总记录数/每页记录数)
3、根据当前页号计算出起始位置:起始位置 = (当前页号-1)*每页记录数
4、用select * from tbl_name limit 起始位置,每页记录数 取得待显示记录
5、列表输出相关信息

在这个流程中,数据库需要两次遍历表才能得到所需数据。尽管limit会在得到指定记录数后会终止遍历,但前面直到“起始位置”的检索是浪费掉的。

这里提出一种新算法与大家讨论:
1、利用mysql的用户变量,分割并提取每页起始的id号。
2、查询结果的记录数即为总页数
3、根据当前页号取得当前页的起始id
4、用select * from tbl_name where id>=起始id limit 每页记录数 取得待显示记录
5、列表输出相关信息

可以看到,在后一次查询中。由于利用了id作为主键的特征,数据库可直接定位到所需记录。从而减少了查询时间。
这个查询算法有一个副产品:可以产生一条用于衔接上下页的重复记录,也就是各页间有一条重叠的记录。当然,去掉他也是很容易的。
以下是测试代码:
include "mysql_result_all.inc"; // 用于显示查询结果的工作函数

$pagesize = 9; // 每页行数
$type = 1; // =1降序排列
$mode = 0; // =1不重复上页最后一条记录

$conn = mysql_connect(); // 连接mysql
mysql_select_db("site"); // 选择数据库

// 准备动态修改查询串
if($type) {
$order = "desc";
$expr = "}else {
$order = "";
$expr = ">=";
}
if($mode) $pagesize++;

mysql_query("set @v:=-1"); // 定义mysql用户变量
$rs = mysql_query("select @v:=(@v+1) as xh, id from data HAVING mod(xh,$pagesize)=0 order by id $order");
mysql_result_all($rs); // 检查各页分布

echo $pages = mysql_num_rows($rs); // 取得总页数
if($mode) $pagesize--;

// 测试分页结果,$i表示显示页
for($i=0;$i mysql_data_seek($rs,$i); // 移动结果集指针
list($xh,$id) = mysql_fetch_row($rs); // 取得起始id
echo "
[$i] $xh -- $id";
$rs1 = mysql_query("select * from data where id$expr$id order by id $order limit $pagesize");
mysql_result_all($rs1); // 显示相关结果
}
?>

mysql_result_all.inc
这个函数我贴过多次了,对调试程序非常有用的。

function mysql_result_all($result,$format="") {
echo "

";
for($i=0;$i echo "";
}
echo "";
while($row = mysql_fetch_row($result)) {
echo "";
for($i=0;$i echo "";
}
echo "";
}
echo "
".mysql_field_name($result,$i)."
".$row[$i]."
";
}
?>

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 Article Tags

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)

Fix event ID 55, 50, 98, 140 disk error in event viewer Fix event ID 55, 50, 98, 140 disk error in event viewer Mar 19, 2024 am 09:43 AM

Fix event ID 55, 50, 98, 140 disk error in event viewer

This Apple ID is not yet in use in the iTunes Store: Fix This Apple ID is not yet in use in the iTunes Store: Fix Jun 10, 2024 pm 05:42 PM

This Apple ID is not yet in use in the iTunes Store: Fix

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products? Mar 12, 2024 pm 07:20 PM

Where can I view the records of things I have purchased on Pinduoduo? How to view the records of purchased products?

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

The operation process of WIN10 service host occupying too much CPU

Where can I find Alibaba ID? Where can I find Alibaba ID? Mar 08, 2024 pm 09:49 PM

Where can I find Alibaba ID?

Where to check Tencent Video ID Where to check Tencent Video ID Feb 24, 2024 pm 06:25 PM

Where to check Tencent Video ID

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

Learn how to handle special characters and convert single quotes in PHP

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

Go language development essentials: 5 popular framework recommendations

See all articles