如何用PHP实现基于MySQL的简单数据分页功能?

更新于
2026-09-26 05:59:04
1阅读来源:SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计337个文字,预计阅读时间需要2分钟。

如何用PHP实现基于MySQL的简单数据分页功能?

PHP实现MySQL表数据简单分页:连接数据库,选择数据库名,设置字符集,查询数据行数。

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的简单数据分页功能?
标签:简单分页P

本文共计337个文字,预计阅读时间需要2分钟。

如何用PHP实现基于MySQL的简单数据分页功能?

PHP实现MySQL表数据简单分页:连接数据库,选择数据库名,设置字符集,查询数据行数。

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的简单数据分页功能?
标签:简单分页P