0

I'm trying to substitute a variable in a db_query but keep on getting this error:

Recoverable fatal error: Argument 2 passed to db_query() must be of the type array, string given

It's a single item string that I'm trying to insert:

$primary_key = db_query("SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'mydatabase') AND (`TABLE_NAME` = '%s') AND (`COLUMN_KEY` = 'PRI')",$mytable)->fetchAll(PDO::FETCH_ASSOC);

I've tried wrapping the variable in an array but then I get no results. How do I substitute my variable here?

1 Answer 1

1

Drupal 7 doesn't use the '%' place holders. Try this:

$primary_key = db_query("SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE (`TABLE_SCHEMA` = 'mydatabase') AND (`TABLE_NAME` = :tbl_name) AND (`COLUMN_KEY` = 'PRI')", array(':tbl_name' => $mytable));
2
  • Got a MySQL error on this one.Looks like it's changing the substitution: "('COLUMN_KEY' = 'PRI'); Array ( [:tbl_name] => store ) in"
    – Ken J
    Commented Mar 24, 2015 at 23:11
  • this example had single quotes ', instead of ` wrapped around the columns Commented Mar 25, 2015 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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