php mysql按查询排序错误
我有posts表的以下属性- id、标题、类别id、内容、发布日期。 对于类别表: 身份证和姓名 我在$query中得到一个错误。=“orderbyposts.id”php mysql按查询排序错误,php,mysql,Php,Mysql,我有posts表的以下属性- id、标题、类别id、内容、发布日期。 对于类别表: 身份证和姓名 我在$query中得到一个错误。=“orderbyposts.id” 我觉得你的查询结构不正确。WHERE子句前面应该有一个空格,orderby也应该有一个空格 $posts = array(); $query = ("SELECT posts.id AS post_id, categories.id AS category_id, title, contents, date_posted, cat
我觉得你的查询结构不正确。
WHERE
子句前面应该有一个空格,orderby
也应该有一个空格
$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);
假设您使用的是php,请尝试使用mysqli。Mysql本身就遭到了本的抨击。这可能是导致错误的原因您知道串联的作用吗?可能是空白?如果(!isset($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}"; // note whitespace before WHERE
}
$query .= " ORDER BY posts.id DESC"; // note whitespace before ORDER BY here
$query = mysql_query($query);