连接数据库
<?php
$conn = mysql_connect('localhost', 'root', 'root') or die('ErrorCode:' . mysql_errno() . ',Error:' . mysql_error());
mysql_select_db('company') or die('ErrorCode:' . mysql_errno() . ',Error:' . mysql_error());
?>
混编数据分页
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>测试分页</title>
</head>
<body>
<?php
include 'conn.php';
$pagesize = 10;
mysql_query("SET NAMES utf8");
$sql = "SELECT count(*) cnt FROM com_info";//取出总的记录条数
$r = mysql_query($sql) or die('error code:' . mysql_errno() . ',error:' . mysql_error());
$row = mysql_fetch_assoc($r);
$total = $row['cnt'];//总的记录条数
$total_pages = ceil(intval($total)/$pagesize);//总页数
$pn = isset($_GET['page']) ? intval($_GET['page']) : 1;
$pn < 1 && $pn = 1;
$pn >= $total_pages && $pn = $total_pages;
$offset = ($pn - 1)*$pagesize;
$sql = "SELECT com,com_name FROM com_info LIMIT {$offset},{$pagesize}";
$r = mysql_query($sql) or die('error code:' . mysql_errno() . ',error:' . mysql_error());
?>
<table>
<tr><th>编号</th><th>公司名</th></tr>
<?php
while ($row = mysql_fetch_assoc($r)) {
?>
<tr><td><?php echo $row['com']?></td><td><?php echo $row['com_name']?></td></tr>
<?php
}
?>
</table>
<div class="page">
<a href="page.php">首页</a>
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<a href="page.php?page=<?php echo $i?>">第<?php echo $i?>页</a>
<?php
}
?>
<a href="page.php?page=<?php echo $total_pages?>">末页</a>
</div>
</body>
</html>