码迷,mamicode.com
首页 > 数据库 > 详细

利用PHP实现MySQL表数据的简单分页

时间:2015-09-04 18:43:42      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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>";
     
?>


利用PHP实现MySQL表数据的简单分页

标签:

原文地址:http://my.oschina.net/dongdong11019/blog/501128

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!