Home >Database >Mysql Tutorial >PHP实现对mysql数据库内容分页显示_MySQL

PHP实现对mysql数据库内容分页显示_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:02:181188browse

<?php

$conn=mysql_connect(&#39;127.0.0.1&#39;,&#39;root&#39;,&#39;&#39;);

mysql_query(&#39;use test&#39;,$conn);
mysql_query(&#39;set names utf8&#39;,$conn);

$perNumber=3; //每页显示的记录数
$page=$_GET[&#39;page&#39;]; //获得当前的页面值
$count=mysql_query("select count(*) from kangbiao"); //获得记录总数
$rs=mysql_fetch_array($count); 
$totalNumber=$rs[0];
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数
if (!isset($page)) {
 $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的记录
$result=mysql_query("select * from kangbiao limit $startCount,$perNumber"); //根据前面的计算出开始的记录和记录数

echo "<table border=&#39;1&#39;>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>age</th>";
echo "<th>grade</td>";
echo "</tr>";
while ($row=mysql_fetch_array($result)) {

echo "<tr>";
 echo "<td>$row[0]</td>"; 
 echo "<td>$row[1]</td>";
 echo "<td>$row[2]</td>";
 echo "<td>$row[3]</td>";  //显示数据库的内容
echo "</tr>";
}
echo "</table>";

if ($page != 1) { //页数不等于1
?>
<a href="02.php?page=<?php echo $page - 1;?>">上一页</a> <!--显示上一页-->
<?php
}
for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面
?>
<a href="02.php?page=<?php echo $i;?>"><?php echo $i ;?></a>
<?php
}
if ($page<$totalPage) { //如果page小于总页数,显示下一页链接
?>
<a href="02.php?page=<?php echo $page + 1;?>">下一页</a>
<?php
} 
?>

运行结果:

\

Statement:
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