SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Easy way to delete multiple rows in a single querie?

    I have some code that deletes records from the database, in a loop.

    However its prety inificient as it makes a seperate database call in every loop whcih is slow.

    PHP Code:
        for ($i=0$i<count($idarray); $i++) 
              
    db_query("delete from clicks where id='$idarray[$i]'"); 
    Is their a more efficient way of doing that? It is posible to delete multiple records in the same database querie?

  2. #2
    SitePoint Enthusiast shn's Avatar
    Join Date
    Jan 2006
    Location
    Munich DE
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    DELETE FROM `clicks` WHERE `id` < count($idarray)
    Patrick

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    UK
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shn
    Code:
    DELETE FROM `clicks` WHERE `id` < count($idarray)
    Surely that would delete all entries where id is less than the number of elements in my array?

    $idarray stores some random id numbers, say "1, 5, 6, 7" and it is only those entries that I want to delete.

  4. #4
    SitePoint Enthusiast shn's Avatar
    Join Date
    Jan 2006
    Location
    Munich DE
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, yes, it would. I misunderstood your example there.

    In that case following should work:
    PHP Code:
    $ids implode(','$idarray);
    db_query("DELETE FROM `clicks` WHERE `id` IN ($ids)"); 
    Patrick


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •