开发者

In MySQL, ' results in error, ` works fine. why?

$query = "SELECT * FROM `users` WH开发者_Go百科ERE `username` = 'admin'";#works

$query = "SELECT * FROM 'users' WHERE 'username' = 'admin'";#does not work

Is this yet another quirk Im going to have to get used to, or is something funny going on?


Single quotes (') and double quotes (") are used to specify strings in MySQL. Backticks (`) are used for column/table references.

Your second query will fail for two reasons:

  1. 'users' specifies a string, not a reference to the table users, and FROM expects a table reference.
  2. 'username' = 'admin' does a string comparison, and the string username is never equal to the string admin.


It's not legal syntax to quote the name of a column with '

The ` (backtick) is used to quote identifiers.

Since none of your columns are reserved keywords, this would work too:

 "SELECT * FROM users WHERE username = 'admin'"


In MySQL, by default single-quotes (') and double-quotes (") are literal string delimiters, while backticks (`) are identifier quotes. If you set the SQL mode to include ANSI_QUOTES, then double-quotes will also be identifier quotes rather than literal string delimiters.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜