PHP MySQL数据分页
时间:2021-07-01 10:21:17
帮助过:3人阅读
html>
<head>
<title>PHP分页
</title>
</head>
<body>
<?php
$dbhost = ‘localhost‘; // 数据库主机
$dbuser = ‘root‘; // 用户名
$dbpass = ‘123456‘; // 密码
$rec_limit = 10; // 每页10条数据
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die(‘连接失败: ‘ . mysqli_error());
}
mysqli_select_db($conn,‘test‘); // 要操作的数据
/* 获取所有的记录数 */
$sql = "SELECT COUNT(id) FROM test ";
$retval = mysqli_query( $conn, $sql );
if(! $retval ) {
die(‘没有获取到数据: ‘ . mysqli_error($conn));
}
$row = mysqli_fetch_array($retval, MYSQLI_NUM );
$rec_count = $row[0];
if( isset($_GET[‘page‘] ) ) {
$page = $_GET[‘page‘] + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT name ".
"FROM test ".
"LIMIT $offset, $rec_limit";
$retval = mysqli_query( $conn, $sql );
if(! $retval ) {
die(‘不能获取到数据: ‘ . mysqli_error($conn));
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) {
echo "TEST name :{$row[‘name‘]} <br> ";
}
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"".$_SERVER[‘PHP_SELF‘]."?page=$last\">上一页</a> |";
echo "<a href = \"".$_SERVER[‘PHP_SELF‘]."?page=$page\">下一页</a>";
}else if( $page == 0 ) {
echo "<a href = \"".$_SERVER[‘PHP_SELF‘]."?page=$page\">下一页</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"".$_SERVER[‘PHP_SELF‘]."?page=$last\">最后一页</a>";
}
mysqli_close($conn);
?>
</body>
</html>
修改每页显示的数据或者插入多条数据查看效果,PHP MySQL插入数据。
PHP MySQL数据分页
标签:多个 用户 php 没有 get col limit 分页 test