我在我的PHP腳本中收到分頁錯誤。查詢在mysql工作臺內部直接運行時可以正常工作,並返回正確的結果。PHP分頁查詢失敗
返回錯誤:您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用近「LIMIT 0,20」在行說明書2
$getpositive = "select case_number, c.name as subject, a.name, u.first_name, u.last_name from cases as c join cases_cstm as cc on c.id = cc.id_c
left join accounts as a on a.id = c.account_id left join users as u on u.id = c.assigned_user_id where rating_c ='1';";
$db -> PS_Pagination($getpositive, 20, 5, "");
$db -> setDebug(true);
$rs = $db->paginate();
$positive_rating_rows = mysql_num_rows($rs);
我然後顯示在一個表中的結果:
while($val = mysql_fetch_assoc($rs))
{
?>
</tr>
<tr>
<td width="7%"><?=$val['case_number']?></td>
<td width="40%"><?=$val['subject']?></td>
<td width="40%"><?=$val['name']?></td>
</tr>
這裏是我的分頁功能:
public function PS_Pagination($sql, $rows_per_page = 10, $links_per_page = 5, $append = "") {
//$this->conn = $connection;
$this->sql = $sql;
$this->rows_per_page = (int)$rows_per_page;
if (intval($links_per_page) > 0) {
$this->links_per_page = (int)$links_per_page;
} else {
$this->links_per_page = 5;
}
$this->append = $append;
$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
if (isset($_GET['page'])) {
$this->page = intval($_GET['page']);
}
}
public function paginate() {
//Check for valid mysql connection
if (! $this->IsConnected()) {
$this->SetError("No connection");
return false;
}
//Find total number of rows
$all_rs = @mysql_query($this->sql);
if (! $all_rs) {
if ($this->debug)
echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
return false;
}
$this->total_rows = mysql_num_rows($all_rs);
@mysql_close($all_rs);
//Return FALSE if no rows found
if ($this->total_rows == 0) {
if ($this->debug)
//echo "Query returned zero rows.";
return FALSE;
}
//Max number of pages
$this->max_pages = ceil($this->total_rows/$this->rows_per_page);
if ($this->links_per_page > $this->max_pages) {
$this->links_per_page = $this->max_pages;
}
//Check the page value just in case someone is trying to input an aribitrary value
if ($this->page > $this->max_pages || $this->page <= 0) {
$this->page = 1;
}
//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page - 1);
//Fetch the required result set
//echo $this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}";
$rs = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}");
if (! $rs) {
if ($this->debug)
echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
return false;
}
return $rs;
}
發佈實際的錯誤消息可能會有所幫助...還有,您的$ db類(特別是PS_Pagination和paginate函數)的摘錄會有所幫助。 – Oldskool 2012-02-09 21:30:06
我添加了錯誤。我不確定分頁函數的位置。 – ipengineer 2012-02-09 21:33:06
顯然其中一個功能是修改您的查詢(至少添加了「LIMIT 0,20」位),所以看到該功能對此負責至關重要。否則,我們不可能想出一個解決方案。 – Oldskool 2012-02-09 21:37:20