Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to run a select query using db_query. Here is the code:

$nid_array = array(24,34,56,7,8);
$list_of_nids = implode(',',$nid_array);
$query = "  SELECT *
            FROM {table_name} as t
            WHERE t.nid IN (:list_of_nids)";
$placeholders = array();
$placeholders[':list_of_nids'] = $list_of_nids;

$result = db_query($query, $placeholders)->fetchAll();

This query does not return any results. Although if I do not use placeholders & replace :list_of_nids with $list_of_nids, then I get proper results.

I believe that when using placeholders the value of :list_of_nids is taken as the $list_of_nids wrapped with single quote, which is causing problem here.

I want to use placeholders. So, how can use it without wrapping the value with single quotes.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You're imploding your array into a string, so PDO will (correctly) put quotes around it. PDO can handle arrays just fine, though, no need for manual manipulation:

$nid_array = array(24,34,56,7,8);

$query = "  SELECT *
            FROM {table_name} as t
            WHERE t.nid IN (:list_of_nids)";

$placeholders = array();
$placeholders[':list_of_nids'] = $nid_array;

$result = db_query($query, $placeholders)->fetchAll();
share|improve this answer

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.