MySQLism: avoid using double quotes for alphanumerical values
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
According to SQL-99, double quotes should only be used to reference a column name or a table name.
Example:
SELECT "type" FROM access WHERE "type" = ...
This query allows the parser to detect that type is a column and not a function or a reserved word.
Unfortunately, MySQL makes an extensive use of double quotes to represent alphanumerical values. MySQL is very tolerant and allows to write:
SELECT foo FROM bar WHERE foo="this value";
Unfortunately, this code is not portable and will fail on other database systems.
You should always use single quotes for alphanumerical values. The correct syntax is:
SELECT foo FROM bar WHERE foo='this value';
In PHP, the advantage is that you may be able to write:
$value = "this value";
$query = "SELECT foo FROM bar WHERE foo='$value';"
So you don't have to escape quote in the PHP code.
This is a small tip to make your code readable!
PostgreSQL does not allow double-quotes in SQL queries. This is because double-quotes are interpreted as escape characters per standard. Instead use only single quotes.
Note also that MySQL accepts backticks for object (table, index, etc.) identifiers. This is non-standard, so PostgreSQL does not support it; instead, use double-quotes if you have to quote a table name (such as having a space in the table name). Better yet, stick to table names which don't require quoting.
Reference: http://drupal.org/node/398252
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion