Is “key” a reserved word in MySqli? I'm getting an error

北城余情 提交于 2020-01-22 02:31:08

问题


I'm just getting into MySql/MySqli really, and I'm using prepared statements. The whole of my script is working fine except this single line:

if ($stmt = $con->prepare("SELECT bandHash, userHash, userPassHash, type FROM account_active WHERE key=?")) {

I found out that's the line by manually tracking it down, then running the mysqli_error($resource) function, and got this result 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=?' at line 1

Please don't worry that it says line 1, that's just because of how it's been executed, it's actually line 280.

So what I'm wondering, because I'm already using statements like this, is "key" a reserved word that I cannot use - or do I need to look elsewhere for my error?

A simple yes or no answer will suffice, thanks!


回答1:


I don't know why you couldn't have looked this up yourself, but, yes, key is a reserved word in MySQL.

You should use backticks with your field names to avoid ever having to worry about this.

SELECT `bandHash`, `userHash`, `userPassHash`, `type`
  FROM `account_active`
 WHERE `key` = ?

This not only solves your problem, but it feels more explicit and (as you can see) aids syntax highlighting.




回答2:


Yes, key is a reserved word

I'd consider renaming it to avoid using the quoted name everywhere

`key`


来源:https://stackoverflow.com/questions/7380156/is-key-a-reserved-word-in-mysqli-im-getting-an-error

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