我大致由以下鏈接創建一個分頁:PHP/MySQL的使用ORDER BY與LIMIT
http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en#toggle
很涼爽。雖然我的查詢有問題。
它工作正常,像這樣:
require 'includes/function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ");
while ($row = mysql_fetch_assoc($query)) {
然而,當我試圖加一個ORDER BY這一點,像這樣:
需要 '包括/ function.php';
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 8;
$startpoint = ($page * $limit) - $limit;
$statement = "cars WHERE deleted = 'no'";
$query = mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint}, {$limit} ORDER BY model DESC");
while ($row = mysql_fetch_assoc($query)) {
或者只是改變這樣的說法:
$statement = "rcr_cars WHERE deleted = 'no' ORDER BY model DESC";
我得到這個錯誤:79
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in filepath/myfile.php on line 79.
行是這一行:
while ($row = mysql_fetch_assoc($query)) {
誰能告訴我如何正確使用ORDER BY ,它讓我感到困惑。 :/
您沒有正確使用它,'ORDER BY'必須出現在'LIMIT'語句之前。切換它們,它應該工作。 –
http://stackoverflow.com/questions/4708708/mysql-order-by-limit –