複製代碼 代碼如下:
header("content-type:text/html;charset=utf-8");
//資料庫連接
$conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8");
//查詢共有多少行資料
$sql1 = "select count(*) from user";
$ret1 = mysql_query($sql1);
$row1 = mysql_fetch_row($ret1);
$tot = $row1[0];
//每頁多少行資料
$length = 5;
//總頁數
$totpage = ceil($tot / $length);
//當前頁數
$page = @$_GET['p'] ? $_GET['p'] : 1;
//limit 下限
$offset = ($page - 1) * $length;
echo "
";
echo "
php padding
";
echo "
//將查詢出來的資料用表格顯示
$sql2 = "select * from user order by id limit {$offset}, {$length}";
$ret2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($ret2)) {
echo "
echo "
"; echo "
"; echo "
| ID | "; echo "
USER | "; echo "
PASS | "; echo "
";
"; echo "
| {$row2['id']} |
{$row2['name']} |
{$row2['pass']} | "; echo "
"; }
";
//上一頁和下一頁
$prevpage = $page - 1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}
//跳轉
echo "
上一頁|下一頁
";
echo "";
核心點:
<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和頁數之間的關係。
<2>上一頁和下一頁的獲得方式,以及臨界點。
http://www.bkjia.com/PHPjc/825276.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825276.htmlTechArticle複製代碼 代碼如下: ?php header("content-type:text/html;charset=utf-8"); //資料庫連接 $conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql...