I have a problem with a question mark parameter in a prepared statement using PDO. The My Query class looks like this (at the moment I'm still adding features such as data restrictions, filtering custom parameters and automatically detecting supported statements for the driver used):
// SQL query class Query { public $attributes; // constructor for this object public function __construct() { if ($arguments = func_get_args()) { $tmp = explode(" ", current($arguments)); if (in_array(mb_strtoupper(current($tmp)), ["ALTER", "DELETE", "DROP", "INSERT", "SELECT", "TRUNCATE", "UPDATE"], true)) { // classify the query type $this->attributes["type"] = mb_strtoupper(current($tmp)); // get the query string $this->attributes["query"] = current($arguments); // get the query parameters if (sizeof($arguments) > 1) { $this->attributes["parameters"] = array_map(function ($input) { return (is_array($input) ? implode(",", $input) : $input); }, array_slice($arguments, 1, sizeof($arguments))); } return $this; } } } }
This is a piece of code that executes a request:
$parameters = (!empty($this->attributes["queries"][$query]->attributes["parameters"]) ? $this->attributes["queries"][$query]->attributes["parameters"] : null); if ($query = $this->attributes["link"]->prepare($this->attributes["queries"][$query]->attributes["query"], [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY])) { if ($query->execute((!empty($parameters) ? $parameters : null))) { return $query->fetchAll(\PDO::FETCH_ASSOC); } }
And here is what I call it in my test code:
$c1->addQuery("lists/product-range", "SELECT * FROM `oc_product` WHERE `product_id` IN (?);", [28, 29, 30, 46, 47]); if ($products = $c1->execute("test2")) { foreach ($products as $product) { print_r($product); } }
The problem is that I just see the first product (this is a test against installing OpenCart with vanilla) with identifier 28. As you can see in my code, if the parameter passed is an array, it is automatically determined by lambda I have a place in the constructor of the Query class therefore it is displayed as a string, for example 28,29,30,46,47 .
Is there a missing parameter in the PDO setup that I am missing? Or maybe there is some kind of error or platform limitation in what I do? I know that there are some restrictions on what PDO can do with regards to arrays, and why I pre-insert all arrays to pass them as a simple string.
Here are some procedures that I saw here in SO, which basically makes up the query string, like WHERE product_id IN ({$marks}) , where $marks dynamically generated using a procedure like str_repeat("?", sizeof($parameters)) , but this is not what I am looking for (I could resort to this if there is no known alternative, but this does not seem like a very elegant solution).
My development environment consists of: Windows 7 x64, PHP 5.4.13 (x86, thread safe), Apache 2.4.4 (x86), and MySQL 5.6.10 x64.
Any hint would be greatly appreciated :)