I'm using PDO to execute a statement with an
IN clause that uses an array for its values:
$in_array = array(1, 2, 3); $in_values = implode(',', $in_array); $my_result = $wbdb->prepare("SELECT * FROM my_table WHERE my_value IN (".$in_values.")"); $my_result->execute(); $my_results = $my_result->fetchAll();
The above code works perfectly fine, but my question is why this doesn't:
$in_array = array(1, 2, 3); $in_values = implode(',', $in_array); $my_result = $wbdb->prepare("SELECT * FROM my_table WHERE my_value IN (:in_values)"); $my_result->execute(array(':in_values' => $in_values)); $my_results = $my_result->fetchAll();
This code will return the item whose
my_value equals the first item in the
$in_array (1), but not the remaining items in the array (2, and 3).
PDO is not good with such things. You need to create a string with placeholders dynamically and insert it into the query, while binding array values the usual way. With positional placeholders it would be like this:
In case there are other placeholders in the query, you could use the following approach (the code is taken from my PDO tutorial):
You could use
array_merge()function to join all the variables into a single array, adding your other variables in the form of arrays, in the order they appear in your query:
In case you are using named placeholders, the code would be a little more complex, as you have to create a sequence of the named placeholders, e.g.
:id0,:id1,:id2. So the code would be:
Luckily, for the named placeholders we don't have to follow the strict order, so we can merge our arrays in any order.