开发者

MySql + InnoDB + PHP Single Quote Discrimination?

开发者 https://www.devze.com 2023-02-14 17:42 出处:网络
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 \' i开发者_高级运维n the column list of the INSERT statement:

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 ' i开发者_高级运维n 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))


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消