PHP从零单排(十四)数据分页显示的原理及实现
分页显示是WEB编程中最频繁处理的环节之一。所谓分页显示,就是通过程序将结果集一段一段的来显示。实现分页显示,需要两个初始参数:每页显示多少记录和当前是第几页。再加上完整的结果集,就可以实现数据的分页显示。至于其他功能,比如上一页、下一页等均可以根据以上信息加以处理得到。
要取得某表中的前10条记录,可以使用如下SQL语句:
SELECT * FROM a_table LIMIT 0,10
要查找第11到第20条记录,使用的SQL语句如下所示:
SELECT * FROM a_table LIMIT 10,10
如要查找第21条到第30条记录,使用的SQL语句如下所示:
SELECT * FROM a_table LIMIT 20,10
以上SQL语句可以看出,每次取10条记录,相当于每个页面显示10条数据,而每次所要取得记录的起始位置和当期页数之间存在着这样的关系:起始位置=(当前页数-1)*每页要显示的记录数。如果以变量$page_size表示每页显示的记录数,以变量$cur_page表示当前页数,那么上述可以用下面所示的SQL语句模板归纳:
select * from table limit ($cur_page-1)*$page_size,$page_size;
这样,就得到了分页情况下获取数据的SQL语句。其中$page_size可以根据实际情况制定为一个定值,实际开发中,当前页面$cur_page可以由参数传入。另外,数据要显示的总页数,可以在记录总数和每页显示的记录数之间通过计算获得。比如,如果总记录数除以每页显示的记录数后,没有余数,那么总页数就是这二者之商。
<?php $host='localhost';$user_name='root';$password='helloworld';$conn=mysql_connect($host,$user_name,$password);if(!$conn){ die('FAIL!'.mysql_error());}mysql_select_db('test');if(isset($_GET['page'])){ $page=$_GET['page'];}else{ $page=1;}$page_size=2;$sql='select * from users';$result=mysql_query($sql);$total=mysql_num_rows($result);if($total){ if($total<$page_size) $page_count=1; if($total%$page_size) { $page_count=(int)($total/$page_size)+1; } else { $page_count=$total/$page_size; }}else{ $page_count=0;}$turn_page='';if($page==1){ $turn_page.='Index | Before |';}else{ $turn_page.='<a href=13-8.php?page=1>Index | <a href="13-8.php?page='.(%24page-1).'">Before</a> |';}if($page==$page_count || $page_count==0){ $turn_page.='Next | Last';}else{ $turn_page.='<a href="13-8.php?page='.(%24page+1).'"> Next </a> | <a href="13-8.php?page='.%24page_count.'"> Last </a>';}$sql='select id,name,sex,age from users limit '.($page-1)*$page_size.','.$page_size;$result=mysql_query($sql) OR die ("<br>ERROR:<b>".mysql_error()."</b><br>SQL:".$sql);?><title>13-8.php</title>
ID |
Name |
Sex |
Age |

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

在mysql中,可利用“ALTER TABLE 表名 DROP INDEX unique key名”语句来删除unique key;ALTER TABLE语句用于对数据进行添加、删除或修改操作,DROP INDEX语句用于表示删除约束操作。

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft