$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']}
";
}
if( $page > 0 ) {
$last = $page - 2;
echo "上一页 |";
echo "下一页";
}else if( $page == 0 ) {
echo "下一页";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "最后一页";
}
mysqli_close($conn);?>