簡體   English   中英

php mysql ORDER BY查詢錯誤

[英]php mysql ORDER BY query error

我有帖子表的以下屬性 - id、title、cat_id、contents、date_posted。 對於類別表:id 和 name。

我在 $query.="ORDER BY posts.id" 中收到錯誤消息;

$posts=array();
$query=("SELECT posts.id AS post_id, categories.id AS category_id, title, contents, date_posted, categories.name AS name FROM posts INNER JOIN categories ON categories.id = posts.cat_id");

if(isset($id))
{
$id=(int)$id;
$query.="WHERE posts.id={$id}";
}

$query.="ORDER BY posts.id DESC";
$query=mysql_query($query);

您的查詢構造對我來說似乎不正確。 WHERE子句之前應該有一個空格, ORDER BY

$posts = array();
$query = ("SELECT posts.id AS post_id, categories.id AS category_id, title, contents, date_posted, categories.name AS name FROM posts INNER JOIN categories ON categories.id = posts.cat_id");

if (isset($id)) {
  $id=(int)$id;
  $query .= " WHERE posts.id={$id}"; // note whitespace before WHERE
}

$query .= " ORDER BY posts.id DESC"; // note whitespace before ORDER BY here
$query = mysql_query($query);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM