MySql + InnoDB + PHP 单引号歧视?
好的,我最近将一些表转换为 InnoDB 存储引擎,每当我在 INSERT 语句的列列表中使用单引号 ' 时,我都会收到错误:
INSERT INTO 'Table' ('Col1', 'Col2') VALUES ('Val1', 'Val2')
当我厌倦使用 phpMyAdmin 生成正确的语句时,它看起来几乎一样,所以我将它粘贴到我的应用程序中,并且它起作用了。然后我的下一个语句出现了同样的错误,所以我开始怀疑。经过一番尝试后,我发现问题在于查询需要反引号而不是单引号,但不是整个事情。
INSERT INTO `Table` (`Col1`, `Col2`) VALUES ('Val1', 'Val2')
有效,所以 MySql 并不是不理解单引号。这是怎么回事?我也不能像以前那样使用 MyISAM 将列不加引号(即:Table(Col1, Col2))
Okay, I've recently converted a number of tables to the InnoDB storage engine and I've been getting errors whenever I use a single-quote ' in the column list of the INSERT statement:
INSERT INTO 'Table' ('Col1', 'Col2') VALUES ('Val1', 'Val2')
When I tired using phpMyAdmin to generate a proper statement, it looked virtually the same, so I pasted it in my app, and it worked. Then my next statement had the same error so I started to get suspicious. After a bit of playing around I found that the problem was that the query needed a back-tick instead of single-quotes, but not for the whole thing.
INSERT INTO `Table` (`Col1`, `Col2`) VALUES ('Val1', 'Val2')
works, so it's not like MySql doesn't understand the single-quote. What is going on here? I also can't just leave the columns unquoted like before with MyISAM (ie: Table(Col1, Col2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为单引号表示字符串文字,而反引号表示数据库/表/列标识符转义。
删除反引号会导致错误,因为
TABLE
是保留字,因此为了将其用作表名,您必须包含反引号。That's because single quotes denote string literals, whereas backticks denote database/table/column identifier escapes.
Removing backticks causes an error because
TABLE
is a reserved word, so in order to use it as a table name you have to include backticks.您可以在字符串值两边加上单引号,并在对象名称两边使用反引号。从技术上讲,反引号是不必要的,除非表/列的名称是保留字或其中包含空格/有趣的字符。
You put single quotes around string values, and you use backticks around object names. The backticks are technically not necessary though unless that name of your table/column is a reserved word or has spaces/funny characters in it.