Search code examples
mysqlmysql-error-1064

mysql_query error if single quotes used


I want to know why

$amzius_sql = "SELECT DISTINCT `Age` , SUBSTRING( Age, LOCATE( ' ', Age ) ) AS `AgePrefix` , SUBSTRING_INDEX( Age, ' ', 1 ) AS `AgeValue` FROM `suoPage` ORDER BY `AgePrefix` , `AgeValue` LIMIT 0 , 30";
$amzius_res = mysql_query($amzius_sql);

works flawlessly and this:

$amzius_sql = 'SELECT DISTINCT `Age` , SUBSTRING( Age, LOCATE( " ", Age ) ) AS `AgePrefix` , SUBSTRING_INDEX( Age, " ", 1 ) AS `AgeValue` FROM `suoPage` ORDER BY `AgePrefix` , `AgeValue` LIMIT 0 , 30';
$amzius_res = mysql_query($amzius_sql);

produces an error: Unknown column ' ' in 'field list'

The only difference is quotes. If both queries are queried in phpMyAdmin - both work, but only the first one works if queried by mysql_query() in PHP


Solution

  • Here is the explaination:

    The ANSI_QUOTES mode causes the server to interpret double-quoted strings as identifiers. Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks.

    Link