我的 php 脚本上有用于分页的代码
<?php
session_start();
//Database stuff here
$image = $_SESSION['image'];
$cat = $_GET['cate'];
$pagenum = $_GET['pagenum'];
//PAGING BEGINS
if(!isset($pagenum)){
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM tbl_product WHERE category = '$cat'");
$rows = mysql_num_rows($data);
$page_rows = 4;
$last = ceil($rows/$page_rows);
if($pagenum < 1)
$pagenum = 1;
elseif($pagenum > $last)
$pagenum = $last;
$max = 'limit '.($pagenum - 1) * $page_rows.','.$page_rows;
$data_p = mysql_query("SELECT * FROM tbl_product WHERE category = '$cat' $max");
while($row = mysql_fetch_array($data_p)) {
echo $row[1];
echo $row[2];
}
echo "<p>";
echo "--Page $pagenum of Last </p>";
if($pagenum == 1){
} else {
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=1'>First</a>";
echo "";
$previous = $pagenum - 1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'><-Previous</a>";
}
if($pagenum == $last){
} else {
$next = $pagenum + 1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next ->></a>";
echo "";
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last</a>";
}
?>
当我运行它时,第一页显示了指定的 4 个项目,但是当我单击“下一步”时,$pagenum 变量变为 0,导致 limit($max) 为负范围,因此出现 SQL 错误。我不知道为什么。我已经尝试了几个分页脚本,并以同样的错误告终。当我尝试移至下一页时,$pagenum 变为 0。请问我的错误是什么?谢谢