Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: "error in your SQL syntax ... near key ..."? [closed]

I found one very cool scirpt for lost password but this row is making me problems

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

error

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 'key, vreme) VALUES ("123123", "1ed2f5100a26298a55b2935cbea7d4a0", "1337991670"' at line 1
like image 672
Благовест Тодоров Avatar asked Dec 01 '22 06:12

Благовест Тодоров


2 Answers

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') ")
like image 91
eggyal Avatar answered Dec 05 '22 07:12

eggyal


key is a reserved keyword - enclose it in backticks

`key`
like image 28
zerkms Avatar answered Dec 05 '22 05:12

zerkms