5

I have this array

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

Where every element of this array is ID of post. In mysql, table posts, column ID.

DELETE query for one ID would be something like

DELETE FROM posts WHERE id = $id

I can make look through this array and make delete query on each of them.

But they are 4. So it will be 4 queries. And If I had 70? 70 queries....

And question is, how to delete all posts at once having this array of IDs?

2
  • 4
    Use WHERE id IN () Commented Jun 23, 2017 at 13:10
  • This is a duplicate of this. Commented Jun 23, 2017 at 13:41

1 Answer 1

17

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.