脚本中的分页错误

时间:2014-03-27 21:03:39

标签: php mysql

我收到了一个奇怪的错误

  

警告:mysql_fetch_array()要求参数1为资源,布线在第71行的/home/u921594465/public_html/category.php中给出。上一页

这是我脚本中的第71行:

while ($row1 = mysql_fetch_array($query)) {
   if (isset($_GET['page'])) {
       $page = preg_replace("#[^0-9]#","",$_GET['page']);
   } else {
       $page = 1;
   }
   $perPage = 5;
   $lastPage = ceil($count / $perPage);

   if ($page < 1) {
       $page = 1;
   } else if ($page > $lastPage) {
       $page = $lastPage;
   }
   $limit = "LIMIT " . ($page -1) * $perPage .", $perPage";
   $query = mysql_query("SELECT * FROM post WHERE cat='$nameID' ORDER BY date DESC    $limit");

   if ($lastPage != 1) {
       if ($page != $lastPage) {
           $next = $page + 1;
           $pagination .= '<a href="/category.php?page='.$next.'">Next</a>';
       } 

       if ($page != 1) {
           $prev = $page - 1;
           $pagination .= '<a href="/category.php?page='.$prev.'">Prev</a>';
       }
   }
   while ($row1 = mysql_fetch_array($query)) {
       //LINE 71 HERE
       $output .= $row1['name'] . '<br />';
   }

1 个答案:

答案 0 :(得分:1)

您的查询返回了一些错误(false),因此在获取数组之前检查您的查询是否正确并执行类似的操作 -

$result = mysql_query($query);

if($result === FALSE) {
    die(mysql_error()); 
}

while($row = mysql_fetch_array($result))
{
    //...code here
}