Is “key” a reserved word in MySqli? I'm gettin

2019-03-05 05:18发布

问题:

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`


标签: php mysql mysqli