I need to return a multidimensional array from a query with the id keys named key. (needed for object keys in amazon S3 API)
example:
Array ( [0] => Array ( [key] => 8 ) [1] => Array ( [key] => 7 ) [2] => Array ( [key] => 6 ) )
The problem: key is a reserved name in MySQL. I have to use the name key.
The following query gives an error
SELECT `file_name` AS key FROM (`uploads`)
but adding backticks around key doesn’t give errors
SELECT `file_name` AS `key` FROM (`uploads`)
Is it safe to continue like this or is there a better way? I was thinking to rename the id key in the array in PHP but doing it in the query seems faster
As stated, use backticks.
From the MYSQL Docs
Reserved words are permitted as identifiers if you quote them as described in Section 9.2, “Schema Object Names”: