我有post表的以下属性 - ID,标题,CAT_ID,内容,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);
答案 0 :(得分:1)
您的查询构造对我来说似乎不正确。在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);