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:
'users'
specifies a string, not a reference to the tableusers
, andFROM
expects a table reference.'username' = 'admin'
does a string comparison, and the stringusername
is never equal to the stringadmin
.
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.
精彩评论