<?php
//数据库连接相关信息
$host = 'localhost';
$dbuser = 'dbuser';
$dbpasswd = 'dbpasswd';
$database = 'database';
//连接数据库,并选择数据库,设置数据库输出的编码
$conn = mysql_connect($host,$dbuser,$dbpasswd) or die('mysql连接失败:'.mysql_error());
mysql_select_db($database);
mysql_query('set names "utf8"'); //设置编码,也是解决输出乱码的问题
if (isset($_GET['page'])){ //判断是否提交
$page = $_GET['page'];
}else {
$page = 1;
}
$page_size = 5; //每页显示五条数据
//获取数据的总量
$sql = 'select * from db_name';
$result = mysql_query($sql);
$total = mysql_num_rows($result);
//echo $total;
//开始计算总页数
if ($total){ //判断返回的数量
if ($total < $page_size) //判断返回的数量,是否小于设置的显示页数个数
$page_count = 1; //是的话,就只显示一页
if ($total % $page_size){ //判断是否有余数
$page_count = (int)($total / $page_size) + 1; //如果是余数,则总页数等于总记录数除以页数取整再+1
}else { //如果没有余数
$page_count = $total / $page_size; //如果没有余数,则就是返回记录数除以设置的页数
}
}else {
$page_count = 0;
}
//翻页连接
$turn_page = '';
if ($page == 1){
$turn_page .= '首页 | 上一页 | ';
}else {
$turn_page .= '<a href="index.php?page=1">首页</a> | <a href="index.php?page='.($page-1).'">上一页</a>' .'|';
}
if ($page == $page_count or $page_count == 0){
$turn_page .= '下一页 | 尾页';
}else {
$turn_page .= '<a href="index.php?page='.($page+1).'">下一页</a> | <a href="index.php?page='.$page_count.'">尾页</a>' .'|';
}
$result = mysql_query('select id,Depart_ID,Station_ID,f_Name from db_name limit '. ($page-1)*$page_size .', '.$page_size) or die('MYSQL执行失败:'.mysql_errno());
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>分页显示信息</title>
</head>
<body>
<table width="500" border="0" align="center">
<tr>
<td>编号:</td>
<td>编码:</td>
<td>线路:</td>
<td>名称:</td>
</tr>
<?php
if ($num = mysql_num_rows($result)){
while ($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Depart_ID']; ?></td>
<td><?php echo $row['Station_ID']; ?></td>
<td><?php echo $row['f_Name']; ?></td>
</tr>
<?php }
}
echo $turn_page; //显示分页
?>
</table>
</body>
</html>
<?php
mysql_close($conn); //关闭数据库
?>