mysql ORDER BY查询错误


php mysql ORDER BY query error

我有以下属性的帖子表-id、标题、cat_id内容,date_posted。对于类别表:Id和名称

我在$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);