php数据库分页

http://www.imooc.com/code/591

";
}


$host = '127.0.0.1';
$user = 'root';
$pass = '';

$dbName = 'sangedaren';

//连接数据库
$link = mysql_connect($host,$user,$pass) or die('数据库连接失败');
mysql_select_db($dbName);

mysql_query("set names 'utf8'");


$page = 4;
$pagesize = 2;



$offset = ($page - 1) * $pagesize;



//分页原理:假如是第三页,每页2个数据,第三页开始的是 (3-1)* 2 = 4,前面有四个数据
//limit m,n 表示从m(这里第4行后)行后取n(这里2行)行数据

$sql = "select * from user limit $offset , $pagesize"; 

$res =  mysql_query($sql);

$data = array();

while ($row = mysql_fetch_array($res,MYSQLI_ASSOC)) {
    $data[] = $row;
}


echo '
';
print_r($data);
echo '
'; ?>

你可能感兴趣的:(php数据库分页)