Reputation: 2033
after 1 afternoon of not finding the right direction I hope somone can help me.
What I want is: Bind an array to a PDO execute statement. Therefore I use the WHERE IN Statement in the PREPARE. So far so good, my Problem now is, nummer is not unique. In my database are more rows with this value. I need a way to limit the query to the first found row where nummer f.e. = 1 and then go to the next value of the array.
Here is the Code (I found here on stackoverflow ;)
$ids = array(1, 2, 3, 7, 8, 9);
$inQuery = implode(',', array_fill(0, count($ids), '?'));
$stmt = $objDb->prepare(
'SELECT *
FROM table
WHERE nummer IN(' . $inQuery . ')'
);
$stmt->execute($ids);
while($row = $stmt->fetch()) {
echo $row['name'] . "<br />";
}
Upvotes: 2
Views: 177
Reputation: 6687
I believe you want to add GROUP BY nummer
after your WHERE
clause.
$stmt = $objDb->prepare(
'SELECT *
FROM table
WHERE nummer IN(' . $inQuery . ')
GROUP BY nummer
'
);
Edit
For Postgres try:
$stmt = $objDb->prepare(
'SELECT DISTINCT ON (nummer) nummer, *
FROM table
WHERE nummer IN(' . $inQuery . ')
'
);
Upvotes: 3