mikepuerto Posted September 26, 2006 Share Posted September 26, 2006 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![code]$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);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/ Share on other sites More sharing options...
Ninjakreborn Posted September 26, 2006 Share Posted September 26, 2006 LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99027 Share on other sites More sharing options...
.josh Posted September 26, 2006 Share Posted September 26, 2006 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... ? Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99031 Share on other sites More sharing options...
mikepuerto Posted September 26, 2006 Author Share Posted September 26, 2006 Yes, the file exists... Just using a querystring to get it... The file looks like:[code]1068109210931097109810991147122013261386[/code]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:[code]1068 deleted 01092 deleted 11093 deleted 21097 deleted 31098 deleted 41099 deleted 51147 deleted 61220 deleted 71326 deleted 81386 deleted 9[/code]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? Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99105 Share on other sites More sharing options...
.josh Posted September 26, 2006 Share Posted September 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99112 Share on other sites More sharing options...
shoz Posted September 26, 2006 Share Posted September 26, 2006 [url=http://www.php.net/file]file()[/url] 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[code]1068 deleted 0 <br>1092 deleted 1<br>1093 deleted 2<br>[/code]You can use [url=http://www.php.net/trim]trim()[/url] or more specifically [url=http://www.php.net/rtrim]rtrim()[/url] to remove the newline[code]foreach ($lines as $line_num => $line) { $line = rtrim($line);[/code]You should also be able to use [url=http://php.net/array_map]array_map[/url] to remove the newlines from each line or perhaps use [url=http://www.php.net/file_get_contents]file_get_contents[/url] and explode instead Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99119 Share on other sites More sharing options...
.josh Posted September 26, 2006 Share Posted September 26, 2006 wow i totally forgot about the newline thingy. :( my bad Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99121 Share on other sites More sharing options...
mikepuerto Posted September 26, 2006 Author Share Posted September 26, 2006 and rtrim worked PERFECTLY! Thank for the help guys! Quote Link to comment https://forums.phpfreaks.com/topic/22123-help-deleting-multiple-rows-help/#findComment-99143 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.