Home > Backend Development > PHP Tutorial > Code implementation of PHP native database paging

Code implementation of PHP native database paging

不言
Release: 2023-04-05 09:56:02
forward
2275 people have browsed it

The content of this article is about the code implementation of PHP native database paging. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php
header("Content-type:text/html;charset=utf-8");
// 连接数据库
$con = mysql_connect("数据库地址","数据库账号","数据库密码");
if (!$con){die(&#39;Could not connect: &#39; . mysql_error());}
 
mysql_select_db("数据库名", $con);
 
// 每页显示条数
$pageLine = 5;
 
// 计算总记录数
$ZongPage = mysql_query("select count(*) from 表名");
 
// 计算总页数
$sum = mysql_fetch_row($ZongPage);
$pageCount = ceil($sum[0]/$pageLine);   
 
// 定义页码变量
@$tmp = $_GET[&#39;page&#39;];
 
 
// 计算分页起始值
$num = ($tmp - 1) * $pageLine;
 
// 查询语句
$result = mysql_query("SELECT 字段  FROM  表名 ORDER BY id DESC LIMIT " . $num . ",$pageLine");
 
// 遍历输出
while($row = mysql_fetch_array($result))
  {
      echo $row[&#39;字段&#39;];
      echo "<br/>";
  }
 
//分页按钮
//上一页
$lastpage = $tmp-1;
//下一页
$nextpage = $tmp+1;
 
//防止翻过界
if (@$tmp > $pageCount) {
    echo "没有那么多页啦,请返回";
}
 
//如果页码大于总页数,则显示没有了
if(@$tmp <= 1){
    echo "<a href=\"fenye.php?page=$nextpage\">下一页</a>";
}else if(@$tmp > 1 && @$tmp < $pageCount){
    echo "<a href=\"fenye.php?page=$lastpage\">上一页</a>";
    echo "<a href=\"fenye.php?page=$nextpage\">下一页</a>";
}else if(@$tmp = $pageCount){
    echo "<a href=\"fenye.php?page=$lastpage\">上一页</a>";
}
 
// 关闭数据库连接
mysql_close($con);
?>
Copy after login


The above is the detailed content of Code implementation of PHP native database paging. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template