0

我的 SQL 查询是SELECT * FROM chat WHERE to = '$user_id' AND client_id = '001' LIMIT 4

由于某种原因,该查询给了我以下错误:
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 'to = '1' AND client_id = '001' LIMIT 4' at line 1

我使用了不同的行并且查询运行得非常好——错误是因为“to”这个词吗?或者这背后还有别的什么?

仅供参考,这里是 PHP:

$user_id = $_SESSION['user_id'];
$client_id = '001';
if (!$query = sql("SELECT * FROM arrowchat WHERE to = '$user_id' AND client_id = '$client_id' LIMIT 4")) {
    echo mysql_error();
} else {
    echo 'success';
}
4

2 回答 2

9

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

to是 mysql 中的保留关键字,您不能在查询中按原样使用它。您需要将其包装在反引号中:

SELECT * FROM chat WHERE `to` = '$user_id' AND client_id = '001' LIMIT 4
于 2013-06-26T20:55:33.840 回答
3

to是保留的 mysql 关键字,您需要像这样使用反引号:

     SELECT * FROM arrowchat WHERE `to` ....

保留关键字

于 2013-06-26T20:55:28.697 回答