Skip to content
Advertisement

How to update all rows where id is in array?

I have an array with some numbers in it, 34, 12 and 23 for example.

Now I want to update all database rows where the id is in the array (when the id is 34, 12 and 23).

How do I accomplish this?

Any help would be appreciated.

Advertisement

Answer

In SQL you can filter WHERE with array by using>

WHERE column IN ('value1','value2','value3')

So you have to implode your array to string separated by commas. Something like this:

$query = sprintf("UPDATE someTable SET anything=%s WHERE id IN ('%s')", $newValue, implode("','",$arrayOfIds));
$conn->query($query);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement