在PHP SQL查询中使用'to'

Ric*_*ges 0 php mysql

我的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';
}
Run Code Online (Sandbox Code Playgroud)

bwo*_*ebi 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
Run Code Online (Sandbox Code Playgroud)