php+mysql分页处理的探讨_PHP
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 "
".mysql_field_name($result,$i)." | ";".$row[$i]." | ";
---|
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











If you find event ID 55, 50, 140 or 98 in the Event Viewer of Windows 11/10, or encounter an error that the disk file system structure is damaged and cannot be used, please follow the guide below to resolve the issue. What does Event 55, File system structure on disk corrupted and unusable mean? At session 55, the file system structure on the Ntfs disk is corrupted and unusable. Please run the chkMSK utility on the volume. When NTFS is unable to write data to the transaction log, an error with event ID 55 is triggered, which will cause NTFS to fail to complete the operation unable to write the transaction data. This error usually occurs when the file system is corrupted, possibly due to the presence of bad sectors on the disk or the file system's inadequacy of the disk subsystem.

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

In Alibaba software, once you successfully register an account, the system will assign you a unique ID, which will serve as your identity on the platform. But for many users, they want to query their ID, but don't know how to do it. Then the editor of this website will bring you detailed introduction to the strategy steps below. I hope it can help you! Where can I find the answer to Alibaba ID: [Alibaba]-[My]. 1. First open the Alibaba software. After entering the homepage, we need to click [My] in the lower right corner; 2. Then after coming to the My page, we can see [id] at the top of the page; Alibaba Is the ID the same as Taobao? Alibaba ID and Taobao ID are different, but the two

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

Where can I check the Tencent Video ID? There is an exclusive ID in the Tencent Video APP, but most users do not know how to check the Tencent Video ID. Next is the graphic tutorial on how to check the Tencent Video ID brought by the editor for users who are interested. Users come and take a look! Tencent Video Usage Tutorial Where to check Tencent Video ID 1. First open the Tencent Video APP and enter the special area through [Personal Center] in the lower right corner of the main page; 2. Then enter the Personal Center page and select the [Settings] function; 3. Then go to Settings page, click [Exit Account] at the bottom; 4. Finally, you can view the exclusive ID number on the page shown below.

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.
