PHP+Mysql分页
曾经写的php分页。现在用不到了。分享出来。<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
//初始化数据库连接
$sqldizhi="地址";
$sqlname="登录名";
$sqlpwd="密码";
$sqlsjk="库名";
// 开始获取数据库连接
$conn = mysql_connect($sqldizhi,$sqlname,$sqlpwd) or die(mysql_error());
// 手动更改客户端编码
mysql_query("set names utf8");
// 选择使用哪一个数据库
mysql_select_db($sqlsjk);
// 获取总的记录数
$sql_total_records = "select count(*) from table1";
$total_records_result = mysql_query($sql_total_records);
$total_records = mysql_fetch_row($total_records_result);
echo "总的记录数位: ".$total_records."<br>";
// 获得总页数,一般来说页面大小事固定的,所以这里暂且定为一页5个数据
$page_size = 3;
$total_pages = ceil($total_records/$page_size);
echo "总页数为: ".$total_pages;
// 通过GET方式获得客户端访问的页码
$current_page_number = isset($_GET['page_number'])?$_GET['page_number']:1;
if($current_page_number<1) {
$current_page_number =1;
}
if($current_page_number>$total_pages){
$current_page_number = $total_pages;
}
echo "要访问的页码为:".$current_page_number;
// 获取到了要访问的页面以及页面大小,下面开始分页
$begin_position = ($current_page_number-1)*$page_size;
$sql = "select * from table1 limit $begin_position,$page_size";
$result = mysql_query($sql);
// 处理结果集
echo "<table border='#CCF solid 1px'><th>Mysql Fixed Assets Table</th>";
echo "<tr><td>DbName</td><td>TableName</td><td>Last_update</td><td>n_Nows</td><td>Clustered_Index_Size</td><td>Sum_od_Other_Index_sizes</td></tr>";
while(($row = mysql_fetch_row($result))){
echo "<tr>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "<td>".$row."</td>";
echo "</tr>";
}
echo "</table>";
// 循环显示总页数
?>
<?php
echo '<a href="mysqll.php?page_number=1">首页</a>';
for($i=1;$i<=$total_pages;$i++){
echo '<a href="./mysqll.php?page_number='.$i.'">第'.$i.'页</a>';
}
echo '<a href="mysqll.php?page_number='.($current_page_number-1).'">上一页</a>';
echo '<a href="mysqll.php?page_number='.($current_page_number+1).'">下一页</a>';
echo '<a href="mysqll.php?page_number='.($total_pages).'">尾页</a>';
// 释放数据连接资源
mysql_free_result($result);
mysql_close($conn);
?>
</body>
</html> MySQL已经被移除了啊。。。要么mysqli,要么PDO
页:
[1]