string(1) "3" [2]=> s...">

PHP - remove many elements from a database with one array of ids

I have this array

array(4) { [0]=> string(1) "1" [1]=> string(1) "3" [2]=> string(1) "4" [3]=> string(1) "5" }

      

Where each element of this array is a message id. In mysql, column is the column id.

DELETE request for one ID would look like

DELETE FROM posts WHERE id = $id

I can go through this array and make a delete request for each one.

But they are equal to 4. So it will be 4 questions. What if I was 70? 70 requests ....

And the question is how to delete all messages at once with this ID array?

+3


source to share


1 answer


First use implode()

to create a string from your array, then use WHERE id IN ()

:

$ids = implode("','", $array);
queryMethod("DELETE FROM posts WHERE id IN ('".$ids."')");

      



PHP Manual: Implode

+9


source







All Articles