My code:
$myArray = implode($myArray, ',');
$sth = $dbh->prepare('SELECT foo FROM bar WHERE ids IN (:ids)');
$sth->bindParam(':ids', $myArray);
$sth->execute();
$result = $sth->fetch();
echo $sth->rowCount();
Always shows a count of 1, but when I skip the parametrization and just add the variable itself in it’s place, I get an accurate count. What’s going on here?
You can’t bind a parameter for the IN clause like that. The $myArray string will only count as one value, like if you did this:
Even though there are three comma delimited values, the database reads them as only one string value.
You need to manually insert the IN list into the query, the old-school way.
There is unfortunately no other way. At least for now.