Jump to content

Deleting many rows in one query ?


Rommeo

Recommended Posts

i have a table like :

 

messages

id message userid

1  hello        3

2  bla          3

3  bla        3

4  hello      3

 

and i have an array that keeps the ids of the messages to be deleted.

$deletemsglist ( 1,2,3 )

 

i was deleting the messages like :

<?php
for ($i = 0;; ) { 
delete from messages where id = $deletemsglist [$i]  and userid =$userid   
}
?>

so what i m wondering is, is it possible to use one query to delete the selected messages ? i m not good at queries but what i mean is like :

 

delete from messages where id (1,2,3) and userid = $userid

 

Thank you in advance.

Link to comment
Share on other sites

First, assuming your ID column is a unique ID, there is no need to specify the user ID. Then your list needs to be in the form 1, 2, 3 -- not an actual array. Then your query will look something like

 

DELETE FROM messages WHERE id IN ($deletelist);

 

ANY time you're constructing a query in a program, and using variables, it's always a good idea to output the query so you can see exactly what the query is, and if you still don't see the problem, submit the query manually via PHPMyAdmin or the MySQL monitor, and see what happens.

Link to comment
Share on other sites

Did you satisfy this -

Then your list needs to be in the form 1, 2, 3 -- not an actual array.

 

And did you do this so that you can see if what was produced matched the examples in the mysql documentation -

ANY time you're constructing a query in a program, and using variables, it's always a good idea to output the query so you can see exactly what the query is, and if you still don't see the problem, submit the query manually via PHPMyAdmin or the MySQL monitor, and see what happens.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.