PHP写的一个比较完善的分页.
记录以备后用.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>PHP分页</title>
<style>
.aa
{
}
</style>
</head>
<body>
<?php
echo "测试MYSQL";
$pageurl='mySqlTest3.php';
$pagesize=3;
$pageindex=0;
if(isset($_GET["pageindex"]))
{
$pageindex=$_GET["pageindex"];
}
$selectedBegin=$pagesize*$pageindex;
$conn = mysql_connect("localhost", "root", "123");
if (!$conn) {
echo "不能连接到mySQL: " . mysql_error();
exit;
}
if (!mysql_select_db("mytest")) {
echo "不能连接到数据表: " . mysql_error();
exit;
}
$sql ="SELECT * from customers";
$result = mysql_query($sql);
$rsnum=mysql_num_rows($result);
echo "<br/>一共有的结果数:$rsnum";
$sql = "SELECT * from customers order by customerid desc limit $selectedBegin,$pagesize";
$result = mysql_query($sql);
if (!$result) {
echo "无法执行SQL语句: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "没有发现任何数据";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo "<hr/>";
echo $row["customerid"];
echo "<br/>";
echo $row["name"];
echo "<br/>";
echo $row["address"];
echo "<br/>";
echo $row["city"];
echo "<hr/>";
}
mysql_free_result($result);
if($rsnum % $pagesize==0)
{
$totalPage=$rsnum/$pagesize-1;
}
else
{
$totalPage=floor($rsnum/$pagesize);
}
$pagenow=$pageindex+1;
$pageTotal=$totalPage+1;
$next=$pageindex+1;
$previous=$pageindex-1;
if($previous<=0)
{
$previous=0;
}
if($next>=$totalPage)
{
$next=$totalPage;
}
echo "<a href='$pageurl?pageindex=0'>第一页</a> ";
echo "<a href='$pageurl?pageindex=$previous'>上一页</a> ";
echo "<a href='$pageurl?pageindex=$next'>下一页</a> ";
echo "<a href='$pageurl?pageindex=$totalPage'>最后一页</a> ";
echo "第 $pagenow 页/共 $pageTotal 页";
?>
</body>
</html>