获取页码,设置每页行数
$page = $_POST["page"];
if($page == ""||$page <= 0){
$page = 1;
}
$pageSize = 15;
打开MySQL数据库
$mysqli = new mysqli($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
执行SQL语句
$result = $mysqli->query("select 1 from record_visitor");
获取数据总行数
$rowCount = $result->num_rows;
//$rowCount = $result->num_rows是mysqli面向对象风格,等同于$rowCount = mysqli_num_rows($result);
获取数据总页数
$pageNum = ceil($rowCount / $pageSize);
if($page > $pageNum){
$page = 1;
}
获取数据的偏移量
$offset = ($page - 1) * $pageSize;
//获取的数据从$offset+1开始,例:$page=0,$offset=0,从第1个数据向后推移;$page=2,$offset=5,从第6个数据向后推移.
获取当前页的数据
$result = $mysqli->query("select ip,referer,href from visitor order by time desc limit $offset,$pageSize");
//从$offset+1个数据开始展现$pageSize个数据,例:数据1-10,$page=0,$offset=0,$pageSize=5,展现:6,7,8,9,10.
展现数据
while ($row = $result->fetch_assoc()) {
echo $row["ip"];
echo $row["referer"];
echo $row["href"];
}
//$row = $result->fetch_assoc()是mysqli面向对象风格,等同于$row = mysqli_fetch_assoc($result);循环输出每行数据.
PHP7-MySQLi在分页中的应用