when binding a comma separated list of id’s to my prepared statement i only get one row returned, whereas I expected 3.
<?php
$dbh = new PDO("mysql:host=127.0.0.1;dbname=database", "user", "password");
$stmt = $dbh->prepare('SELECT * FROM Person WHERE PersonID IN (:p)');
$stmt->bindValue(":p", "3,4,5");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<pre>
<?php print_r($result); ?>
</pre>
if I change the stmt to
SELECT * FROM Person WHERE PersonID IN (3,4,5)
i get back 3 rows as expected, I’m confused !
Because bind will essentially wrap that in quotes and treat it as a single value. Then MySQL is converting it back to an integer so it still finds a match for the first item.
You need to do IN (:p1, :p2, :p3) and bind value each separately