Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts

    mysql delete record problem

    what is wrong with the below code?
    for example, i have 10 rows in files2
    5 rows fulfil this condition ($filetime < $oldest)
    no matter what, 1 row get deleted instead of 5. to delete the 5 affected rows, i need to run the below code 5 times.
    i believe it's something to do with my while loop statement
    please assist. thank you.

    PHP Code:
            $q $this->sql->query("SELECT * FROM `files2`");
            
    $t $this->sql->count_rows($q);
            if (
    $t >= 1) {
                while (
    $r $this->sql->fetch_row($q)) {
                    
    $filetime $r['time'];
                    
    $oldest time() - $this->ttl*60;
                    if (
    $filetime $oldest) {
                        
    $this->sql->query("DELETE FROM `files2` WHERE `time`='$filetime'");
                    }
                }
            } 
    Last edited by afdah; 09-20-2011 at 10:07 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    You won't need a while loop for this.

    With a delete query you delete all the rows matching your pattern.
    So like i see it: Every row needs to be deleted the first time you run the query.

  • Users who have thanked Wanna for this post:

    afdah (09-20-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wanna View Post
    You won't need a while loop for this.

    With a delete query you delete all the rows matching your pattern.
    So like i see it: Every row needs to be deleted the first time you run the query.
    i know what you mean. the revised code below fulfil my requirement.
    thank you. :-)

    PHP Code:
            $q $this->sql->query("SELECT * FROM `files2`");
            
    $t $this->sql->count_rows($q);
            if (
    $t >= 1) {
                
    $oldest time() - $this->ttl*60;
                
    $this->sql->query("DELETE FROM `files2` WHERE `time`<'$oldest'");
            } 


  •  

    Tags for this Thread

    Posting Permissions

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