x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10Hosting Member

chappill's Avatar

Join Date: Feb 2008
Posts: 74
Credits: 1,162
chappill is on a distinguished road
Quote  
09-25-2008, 02:59 AM
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.
Reply With Quote
chappill is offlineReport Post
Account Manager

sunils's Avatar

Join Date: Jan 2008
Posts: 2,023
Credits: 30,523
sunils has a reputation beyond repute
Location: Chennai ,India

Send a message via AIM to sunils Send a message via Yahoo to sunils
Quote  
09-25-2008, 03:10 AM
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.
__________________
Sunil Sankar
-------------------------------------------------------------------------
Account Manager

Reply With Quote
sunils is offlineReport Post
x10Hosting Member

Join Date: May 2008
Posts: 53
Credits: 405
scopey is on a distinguished road
Quote  
09-25-2008, 04:10 AM
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.
__________________
- When in doubt, refer to the PHP manual.

Last edited by scopey; 09-25-2008 at 04:12 AM.
Reply With Quote
scopey is offlineReport Post
x10Hosting Member

chappill's Avatar

Join Date: Feb 2008
Posts: 74
Credits: 1,162
chappill is on a distinguished road
Quote  
09-25-2008, 12:19 PM
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...
__________________
http://www.vpc-hack.net
The worlds best online hacking simulation game.

Last edited by chappill; 09-26-2008 at 11:25 AM.
Reply With Quote
chappill is offlineReport Post
x10Hosting Member

chappill's Avatar

Join Date: Feb 2008
Posts: 74
Credits: 1,162
chappill is on a distinguished road
Quote  
09-27-2008, 05:19 PM
Re: How to delete multiple MySQL rows...?

Is this even possible?
__________________
http://www.vpc-hack.net
The worlds best online hacking simulation game.
Reply With Quote
chappill is offlineReport Post
x10 Sophmore

Salvatos's Avatar

Join Date: Jun 2006
Posts: 226
Credits: 2,052
Salvatos has a spectacular aura about
Location: Québec, Canada

Quote  
09-27-2008, 07:05 PM
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 07:06 PM.
Reply With Quote
Salvatos is offlineReport Post
x10 Sophmore

Join Date: Apr 2008
Posts: 145
Credits: 1,025
phpasks will become famous soon enough
Quote  
09-28-2008, 10:59 AM
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
Reply With Quote
phpasks is offlineReport Post
x10 Sophmore

natsuki's Avatar

Join Date: Sep 2008
Posts: 110
Credits: 1,305
natsuki will become famous soon enough
Quote  
09-28-2008, 02:24 PM
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 02:25 PM.
Reply With Quote
natsuki is online nowReport Post
x10Hosting Member

Join Date: Mar 2008
Posts: 7
Credits: 306
seanliu is on a distinguished road
Quote  
09-29-2008, 09:19 AM
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
Reply With Quote
seanliu is offlineReport Post
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
New Site-Suggestions? mnoutside Review My Site 9 08-27-2008 08:01 AM
WP mysql error : Error establishing a database connection orangpelupa Free Hosting 2 12-19-2007 06:08 AM
MySQL server address HELP :S rinnerz.com Free Hosting 5 07-09-2006 11:25 AM


All times are GMT -5. The time now is 06:45 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

Loans | Internet Advertising | American Flags | Mortgage Calculator | Mortgage Calculator