添加LIMIT后,OrderBy查询Php不起作用

时间:2013-09-05 12:55:54

标签: php limit

我想我的问题只是一个语法错误或SORT BY应该在LIMIT之前,无论如何将LIMIT添加到我的查询之后,以下语句会生成一个mysql错误。

$query_pag_data = "SELECT * FROM Apartment LIMIT $start, $per_page";// without LIMIT the if statement works while with LIMIT it doesn't.

if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District" ||) {
$query_pag_data .= "ORDER BY ".$_GET['SortBy']; // It doesn't work if I add LIMIT to my query
}

错误是什么,如何使用LIMIT和 ORDER BY wout改变了我的逻辑。

2 个答案:

答案 0 :(得分:1)

这将起作用

if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
  $query_order_by= " ORDER BY ".$_GET['SortBy']; 
 }
 $query_pag_data = "SELECT * FROM Apartment $query_order_by LIMIT $start, $per_page";

答案 1 :(得分:0)

您完成的SQL应该与SELECT, FROM, ORDER BY, LIMIT类似。所以你的PHP应该写成:

$query_pag_data = "SELECT * FROM Apartment";

if ($_GET['SortBy']=="Price" || $_GET['SortBy']=="District") {
    $query_pag_data .= " ORDER BY ".$_GET['SortBy'];
}

$query_pag_data .= " LIMIT $start, $per_page"