默默小谈PHP@MYSQL分页原理及实现
默默小谈PHP&MYSQL分页原理及实现
在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦。
作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,在一页显示就可以了,可是假如你的网站是几千甚至几十万会员的话,如果都在一页打开的话无论对浏览器还是观看者都是一种折磨。
相信每个学习PHP的新手都会对分页这个东西感觉很头疼,不过有了默默的这一水帖,你肯定会拍拍脑袋说,嘿,原来分页竟然如此简单?的确,现在请深呼吸一口新鲜的空气,仔细的听默默给你一点一点的分解。
假设我们要处理1000条数据,要在每页中显示10条,这样的话就会分100页来显示,咱们先看一看在mysql里提取10条信息是如何操作的。
Select * from table limit 0,10
上面是一句很简单的mysql查询语句,它的作用是从一个名叫table的表里提取10条数据,并且把所有字段的值都获得。
关键的地方就在这段“limit 0,10”,它其中的0是以0为起始点,后面的10则是显示10条数据,那么我们要以10为起始点,显示到第20条数据该怎么写呢?
可能很多大大会心直口快的说“limit 10,20”嘛!啊哦,这样可就错误了哦,正确的写法是“limit 10,10”它后面的参数并非是结束点而是要提取的数目,记住哦。
懂得了如何提取10条数据,那么提取1000条也就是做100次这种查询呀,就是说要做如下的查询:
Limit 0,10 //第一页
Limit 10,10 //第二页
Limit 20,10 //第三页
Limit 30,10 //第四页
……
看出有什么规律了吗?没错,第一个参数每翻一页就增加10,可是第二个参数是不变的。
也就是说咱们设法根据页数来改变第一个参数的值,就可以进行分页显示数据了,怎么样,原理是不是很简单?
可是要怎么设法根据页数来改变第一个参数的值呢?首先,咱们要有一个页数的值,用url的GET方式获取。
比如index.php?page=18
相信大部分的大大对这个东西不陌生吧,这种url地址可是随处可见,其中的page参数的作用就是传入要显示的页数。
咱们通过一段代码来看一看究竟是如何实现的吧:
复制PHP内容到剪贴板PHP代码:
/*
Author:默默
Date :2006-12-03
*/
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10; //每页显示10条数据
$db=mysql_connect("host","name","pass"); //创建数据库连接
$select=mysql_select_db("db",$db); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据数除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/
$total=mysql_num_rows(mysql_query("select id from table")); //查询数据的总数,id是数据库中的一个自动赋值的字段
$pagenum=ceil($total/$num); //获得总页数
//假如传入的页数参数大于总页数,则显示错误信息
$totalrecord=mysql_num_rows($result);
$pagesize=10;
$page=isset($_GET['page'])?$_GET['page']:1;
$pagecount=($totalrecord % $pagesize==0)?$totalrecord / $pagesize:(int) ($totalrecord / $pagesize)+1;
$page=($page>$pagecount || $page
$start=$pagesize*($page-1);
$res=mysql_query("select * from tablename order by id desc limit $start,$pagesize")
//获取相应页数所需要显示的数据,name是数据里的一个字段
While($rsmysql_fetch_array($res)){
Echo $rs['name']."
";
} //显示数据
下面就用for了,就不说了哒.还其它很多办法.万变不离其中.
/*显示分页信息,假如是当页则显示粗体的数字,其余的页数则为超连接,假如当前为第三页则显示如下
1 2 3 4 5 6
*/
?>
假如你仔细的读过上面的代码,把数据库连接和查询的表替换成你的,那么就能看见它的执行效果哦。

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

AI Hentai Generator
Generate AI Hentai for free.

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



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.
