php+mysql数据分页显示

//创建连接
$mysqli=new mysqli('localhost','root','','message');
if(mysqli_connect_errno())
{
    print('连接失败\n');
    exit();
}
$mysqli->query("set names 'utf8'");//编码强制转换,否则中文显示乱码
//获取当前页数
if(isset($_GET['page']))
{
    $page=intval($_GET['page']);
}
else
{
    $page=1;
}
//每页数量
$page_size=3;
//获取总数据量
$result=$mysqli->query("select count(*) as amount from user");
list($amount)=$result->fetch_row();
//计算总共有多少页
if($amount)
{
    if($amount<$page_size)
    {
        $page_count=1;
    }
    else if($amount%$page_size)//有余数
    {
        $page_count=(int)($amount/$page_size)+1;
    }
    else
    {
        $page_count=$amount/$page_size;
    }
}
else
{
    $page_count=0;
}
$str="select userId,usrname,password,createAt from user order by userId limit ".($page-1)*$page_size.",".$page_size;
$result=$mysqli->query($str);
while(list($userId,$usrname,$password,$createAt)=$result->fetch_row())
{
    echo "编号:".$userId."   名称:".$usrname."  密码:".$password."  时间:".$createAt."
"
; } $result->close(); $mysqli->close(); //翻页连接 $page_string=''; if($page==1) { $page_string.='第一页|上一页|'; } else{ $page_string.='第一页|.($page-1). '>上一页|'; } if(($page==$page_count)||($page_count==0)) { $page_string.='下一页|尾页'; } else { $page_string.='.($page+1).'>下一页|.$page_count.'>尾页'; } echo "共".$amount."条数据"; echo $page_string; ?>

你可能感兴趣的:(php+mysql数据分页显示)