Jump to content


Photo

HELP! deleting multiple rows HELP!


  • Please log in to reply
7 replies to this topic

#1 mikepuerto

mikepuerto
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 26 September 2006 - 03:45 PM

Can anyone tell me why this code will only delete 1 row? there are about 2000 lines in the "$file" that i'm cross referencing to delete the rows... Please help! this is driving me nuts!

$file = $_GET['file'];

$lines = file($file);

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to the database');
mysql_select_db($dbname);
	
foreach ($lines as $line_num => $line) {

	$query = "DELETE FROM largo1 WHERE id='$line'";
	$result = mysql_query($query);
	echo "$line deleted $line_num<br>";
	
	
}
mysql_close($conn);


#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 26 September 2006 - 03:46 PM

LIMIT 1

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 September 2006 - 03:55 PM

you sure that $file exists or that $lines holds the file that you are expecting? does your echo statement in your foreach loop echo out 2000 lines from the file, saying "blah deleted 0" "blah deleted 1" etc... ?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 mikepuerto

mikepuerto
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 26 September 2006 - 05:54 PM

Yes, the file exists... Just using a querystring to get it... The file looks like:

1068
1092
1093
1097
1098
1099
1147
1220
1326
1386

and yes, $lines is correct... The script appears to run flawlessly, i get no errors! And to answer your question about my echo's out put, yes it looks perfect! This is what the output looks like:
1068 deleted 0
1092 deleted 1
1093 deleted 2
1097 deleted 3
1098 deleted 4
1099 deleted 5
1147 deleted 6
1220 deleted 7
1326 deleted 8
1386 deleted 9

The first number is obviously the "id" that identifies the row i want to disappear.  At first i thought i actually removed a record... But it's actually not doing anything at all... Could this be some sort of mysql config parameter that only allows for a single DELETE to be used at once or something?

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 September 2006 - 06:02 PM

hmm. i assume that since you aren't getting your die message from your connect, that it's connecting.  add a die statement to your db select:

mysql_select_db($dbname) or die(mysql_error());

maybe the db is not being selected.  also make sure your table name is spelled correctly in your query string.  you might also want to try echoing $query and using one of the echoed strings directly into the database like through phpmyadmin or something.

also, your query is using $line which is your actual data; are you sure that's what the id is supposed to be, in your query? i think that's what you're shooting for, but i'm just making sure.  i saw that you originally had $line_num in your query and then you edited your post.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 26 September 2006 - 06:11 PM

file() does not remove the "newline" from the end of each line. If you look at the source of the output it probably looks similar to the following

1068
 deleted 0 <br>
1092
 deleted 1<br>
1093
 deleted 2<br>

You can use trim() or more specifically rtrim() to remove the newline
foreach ($lines as $line_num => $line) {
    $line = rtrim($line);

You should also be able to use array_map to remove the newlines from each line or perhaps use file_get_contents and explode instead

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 September 2006 - 06:15 PM

wow i totally forgot about the newline thingy. :( my bad
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 mikepuerto

mikepuerto
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 26 September 2006 - 06:49 PM

and rtrim worked PERFECTLY! Thank for the help guys!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users