Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php mysql按查询排序错误_Php_Mysql - Fatal编程技术网

php mysql按查询排序错误

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

我有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, 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);