Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

First post here :)

Here's a valid MySQL query: SELECT "'a','b'" AS FieldStrArray;

(To enter as a string in PHP, remember the backslashes before double quotes.)

At least in Drupal 7's db_query(), this produces a fatal PDO exception, complaining that 'a','b' is not a known field.

My question: how do I work around this apparent limitation of db_query()/PDO?

(Yes, I need a constant string array as one of the field results. This is a minimal replication of the problem... :) )

Any suggestions MOST welcome... including a) whether this is also a problem in PHP itself, b) how to report this as a bug to the Appropriate Authorities, c) Any creative ideas for a workaround assuming it is a real bug.

Thanks!

share|improve this question

1 Answer

Not sure if this is a bug or not (I've seen weird things with single and double quote in PDO queries before), but either way you can work around it by using single quotes to wrap the expression:

$sql = "SELECT '\'a\',\'b\'' AS FieldStrArray";
$val = db_query($sql)->fetchField();
// $val == "'a','b'"
share|improve this answer
Hooray! Thanks, Clive. Double-apostrophe quoting works too: $sql = "SELECT '''a'',''b''' AS FieldStrArray"; – MrPete Apr 20 at 23:53
1  
I would say take the slashes over the double apostrophe. Easier to read when you or someone else comes back to it later. – rooby Apr 21 at 0:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.