the following short test script
<?php
define('DBHOST', '/tmp');
define('DBNAME', 'XXX');
define('DBUSER', 'XXX');
define('DBPASS', 'XXX');
$ids = array('OK251562715876', 'OK178469380239');
try {
$db = new PDO('pgsql:host=' . DBHOST . '; dbname=' . DBNAME, DBUSER, DBPASS, $options);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $db->prepare('select * from pref_money where id in ( ? )');
$sth->execute($ids);
while ($row = $sth->fetch(PDO::FETCH_ASSOC))
print_r($row);
} catch (Exception $e) {
exit('Database problem: ' . $e->getMessage());
}
?>
fails with the error:
Database problem: SQLSTATE[42P18]:
Indeterminate datatype: 7
ERROR: could not determine data type of parameter $2
If I remove the 2nd element of the array, then it works ok.
Is there maybe a way to make the binding work for array with several elements?
I know that I can prepare a complete SQL statement string by using join() etc. and omiting the question mark, but then I need extra effort to prevent SQL injection in my web script…
Using PostgreSQL 8.4.6 with PHP 5.1.6 under CentOS 5.5
Build the query with the right number of question marks- to match the length of the array- then pass the contents of the array as the values to
execute.