MySQL: “error in your SQL syntax … near key …”? [closed]

会有一股神秘感。 提交于 2019-11-28 00:19:46
eggyal

KEY is a Reserved Word. As documented under Schema Object Names:

If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. (Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.)

[ deletia ]

The identifier quote character is the backtick (“`”):

mysql> SELECT * FROM `select` WHERE `select`.id > 100;

If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:

mysql> CREATE TABLE "test" (col INT);
ERROR 1064: You have an error in your SQL syntax...
mysql> SET sql_mode='ANSI_QUOTES';
mysql> CREATE TABLE "test" (col INT);
Query OK, 0 rows affected (0.00 sec)

Therefore:

mysql_query("INSERT INTO `keys` (username, `key`, vreme) VALUES ('$user', '$acckey', '$keyexp') ")

key is a reserved keyword - enclose it in backticks

`key`
 $r = mysql_query("INSERT INTO `keys` (username,`key`,vreme) VALUES ('$user', '$acckey','$keyexp')") or die(mysql_error());

Thank you all (:

also remove the double quotes within the query and encapsulate the query within double instead of single quotes.

use single quotes in the query to encapsulate values.

$r = mysql_query("INSERT INTO `keys` (username,key,vreme) VALUES ('".$user."', '".$acckey."','".$keyexp."')") or die(mysql_error());

Are you missing a single quote at the end of your query between the last close bracket and semi-colon?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!