+ Reply to Thread
Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By chappill
  • 1 Post By seanliu

Thread: How to delete multiple MySQL rows...?

  1. #1
    chappill's Avatar
    chappill is offline x10Hosting Member chappill is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    74

    How to delete multiple MySQL rows...?

    PHP Code:
    PHP Code:
     $slavesel mysql_query("SELECT * FROM `slaves`");
    $result mysql_num_rows($slavesel);
    $randomdel rand(2,$result); 
    </div> Is some code I have for selecting the rows from a table, counting them and randomizing them between 2 and the result.
    What I have after that is:

    <div class="code_header">PHP Code:
    PHP Code:
    $delete mysql_query("DELETE FROM slaves AMOUNT '$randomdel'"); 
    But this doesn't work... It was a guess at it actually happening and much to my non suprisment... It didn't work. Is there anything like this that may work (Something that will delete an amount of rows randomly so instead of starting at 1 all the time it may start at row 34288 if there were that many).
    I was thinking keep what I have at the top then below that have:

    <div class="code_header">PHP Code:
    PHP Code:
    $rand2 rand(1,$randomdel); 
    Then having:

    <div class="code_header">PHP Code:
    PHP Code:
    $delete mysql_query("DELETE FROM slaves LIMIT $rand2$randomdel"); 
    All help will be much appreciated!
    http://www.vpc-hack.net
    The worlds best online hacking simulation game.

  2. #2
    sunils's Avatar
    sunils is offline x10 Spammer sunils is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Chennai ,India
    Posts
    2,264

    Re: How to delete multiple MySQL rows...?

    do you want to randomly delete the rows? and how much rows you want to delete. Sorry i cant understand your question.
    [LEFT][B]Sunil Sankar
    -------------------------------------------------------------------------

  3. #3
    scopey is offline x10Hosting Member scopey is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    62

    Re: How to delete multiple MySQL rows...?

    I'm also lost... If you want to delete rows 1 to random number, you can use "DELETE * FROM slaves LIMIT 1, {$randomdel}" where the first number after the 'LIMIT' indicates the start, and the second indicates the amount.
    Last edited by scopey; 09-25-2008 at 03:12 AM.
    - When in doubt, refer to the PHP manual.

  4. #4
    chappill's Avatar
    chappill is offline x10Hosting Member chappill is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    74

    Re: How to delete multiple MySQL rows...?

    I need it to randomly delete a random amount of rows... At the moment I now have:
    $delete = mysql_query("DELETE FROM `slaves` LIMIT {$rand2},{$randomdel}");
    Which seams to be working, if it works tomorrow I'll award scopey with 250 credits =)

    Not working >.< please help...
    Last edited by chappill; 09-26-2008 at 10:25 AM.
    http://www.vpc-hack.net
    The worlds best online hacking simulation game.

  5. #5
    chappill's Avatar
    chappill is offline x10Hosting Member chappill is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    74

    Re: How to delete multiple MySQL rows...?

    Is this even possible?
    karimirt47 likes this.
    http://www.vpc-hack.net
    The worlds best online hacking simulation game.

  6. #6
    Salvatos's Avatar
    Salvatos is offline x10 Lieutenant Salvatos is an unknown quantity at this point
    Join Date
    Jun 2006
    Location
    Québec, Canada
    Posts
    271

    Re: How to delete multiple MySQL rows...?

    I think that should work but I have no guarantee.

    Code:
    // First get the number of rows you have
    $slavesel = mysql_query("SELECT * FROM slaves");
    $result = mysql_num_rows($slavesel);
    $randomdel = rand(2,$result); // Select your random amount of rows to delete
    $limit_top = $result; // Set your upper limit
     
    // The first row to delete is selected randomly
    $limit1 = rand(0,$limit_top);
     
    // If the amount of rows to delete added to the first limit exceeds the upper limit, set the upper limit as the last row to delete
    if ($limit1 + $randomdel > $limit_top) {
    $limit2 = $limit_top;
    }
    else {
    $limit2 = $limit1 + $randomdel;
    }
     
    // Then proceed to deletion
    $delete = mysql_query("DELETE FROM slaves LIMIT '$limit1','$limit2'");
    PLEASE NOTE
    I'm not sure but I think this line should be changed since MySQL begins counting at zero, but I'd like to have someone else confirm.
    Code:
    $limit_top = $result; // Set your upper limit
    Code:
    $limit_top = $result - 1; // Set your upper limit
    Last edited by Salvatos; 09-27-2008 at 06:06 PM.

  7. #7
    phpasks is offline x10 Sophmore phpasks is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    145

    Re: How to delete multiple MySQL rows...?

    best option truncate table means all data lost in one query - load time minimum.

    TRUNCATE TABLE tablename;
    Asif Khalyani
    http://www.phpasks.com

  8. #8
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: How to delete multiple MySQL rows...?

    You could use salvatos code and then have an iterator like $rows = total number of rows to delete
    for ($i = 0; $i < $rows; i++; ) one then put the code in it where it will randomly choose rows to delete and do the deletion. If you truncate table though the whole table is dropped.
    Last edited by natsuki; 09-28-2008 at 01:25 PM.

  9. #9
    seanliu is offline x10Hosting Member seanliu is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    7

    Re: How to delete multiple MySQL rows...?

    i think you have to specify the row(s) to be deleted using the "where" command

    $delete = mysql_query("DELETE FROM slaves WHERE id>$randomdel");

    or something like that
    dinomirt96 likes this.

+ Reply to Thread

Similar Threads

  1. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  2. WP mysql error : Error establishing a database connection
    By orangpelupa in forum Free Hosting
    Replies: 2
    Last Post: 12-19-2007, 05:08 AM
  3. MySQL server address HELP :S
    By rinnerz.com in forum Free Hosting
    Replies: 5
    Last Post: 07-09-2006, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers