尝试执行JOINed查询时抛出MySQL错误消息

时间:2012-08-01 03:11:52

标签: php mysql

  

可能重复:
  Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

这是代码:

$result=mysql_query("
SELECT items.items_id, 
COUNT(ratings.item_id) AS TotalRating,
AVG(ratings.rating) AS AverageRating
FROM 'items'
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
WHERE ratings.item_id = '{$item_id}' ;");

echo "Error message = ".mysql_error();

while($row=mysql_fetch_assoc($result)) {
      $output[]=$row;
}

这是错误:

Error message = You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''items' LEFT JOIN ratings ON (ratings.item_id = items.items_id) WHERE ' at line 4
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/k/i/c/kickinglettuce/html/Kickinglettuce/ratethis/get_ratings.php on line 38
null

我已经确认$ item_id是基于echo语句的正确响应。

1 个答案:

答案 0 :(得分:2)

您在SINGLE QUOTES(')中使用了表items而不是BACKTICKS(`)。