利用PHP实现MySQL表数据的简单分页
发布时间:2020-05-25 07:41:56 所属栏目:PHP 来源:互联网
导读:利用PHP实现MySQL表数据的简单分页
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 PHP实现MySQL表数据的简单分页<?php
$conn=mysql_connect("127.0.0.1","root",'123456') or die("数据库连接失败");
mysql_select_db("ym");
mysql_query("set names utf8");
//获取数据的行数
$all=mysql_num_rows(mysql_query("select * from t1"));
//定义分页所需的参数
$lenght=5; //每页显示的数量
@$page=$_GET['page']?$_GET['page']:1; //当前页
$offset=($page-1)*$lenght; //每页起始行编号
$allpage=ceil($all/$lenght); //所有的页数-总数页
$prepage=$page-1; //上一页
if($page==1){
$prepage=1; //特殊的是当前页是1时上一页就是1
}
$nextpage=$page+1;
if($page==$allpage){
$nextpage=$allpage; //特殊的是最后页是总数页时下一页就是总数页
}
$sql="select * from t1 order by id limit {$offset},{$lenght}";
$rest=mysql_query($sql);
echo "SQL语句:".$sql."<br/>";
echo "总页数是:".$all."页<br/>";
echo "当前页是第:".$page."<br/>";
echo "<center><table width=500 border=1px />";
while($detail=mysql_fetch_row($rest)){
// echo "<pre>";
// print_r($detail);
// echo "</pre>";
echo "<tr/>";
echo "<td>$detail[0]</td>";
echo "<td>$detail[1]</td>";
echo "<td>$detail[2]</td>";
echo "<tr/>";
}
echo "</table></center>";
echo "<center><a href='code8.php?page=1'>首页|";
echo "<a href='code8.php?page={$prepage}'>上一页</a>|";
echo "<a href='code8.php?page={$nextpage}'>下一页</a>|";
echo "<a href='code8.php?page=$allpage'>末页</center>";
?>
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
