1

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.

4

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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