nadz Posted June 19, 2007 Share Posted June 19, 2007 Basically ive got a list of 18000 username and email addresses in a txt file in the following form: [username] : [username] : [username] : [username] : etc etc etc The same email addresses and usernames are stored in the mysql db on my server. what im trying to do is delete all the rows in my database that match any email addresses in the txt file. for example if there is a line in the text file that says username : myname@myclient.com i want it to delete the mysql row that has "myname@myclient.com" in the email field. any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/ Share on other sites More sharing options...
chigley Posted June 19, 2007 Share Posted June 19, 2007 <?php $lines = file("yourtextfile.txt"); foreach($lines as $content) { list($username, $email) = explode(":", $content); $query = mysql_query("DELETE FROM table WHERE email = '{$email}'"); if($query) { echo "Deleted email address: {$email}"; } else { $error = mysql_error(); echo "Error deleting email address: {$email} - {$error}"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277840 Share on other sites More sharing options...
nadz Posted June 19, 2007 Author Share Posted June 19, 2007 thanks ill test that now Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277860 Share on other sites More sharing options...
nadz Posted June 19, 2007 Author Share Posted June 19, 2007 My first post was a lil dodgy, this is what it should look like. Basically ive got a list of 18000 username and email addresses in a txt file in the following form: [username] : [email] [username] : [email] [username] : [email] [username] : [email] etc etc etc The same email addresses and usernames are stored in the mysql db on my server. what im trying to do is delete all the rows in my database that match any email addresses in the txt file.for example if there is a line in the text file that says myusername : myname@myclient.com i want it to delete the mysql row that has "myname@myclient.com" in the email field. any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277865 Share on other sites More sharing options...
chocopi Posted June 19, 2007 Share Posted June 19, 2007 Thats what Chigley has posted The only thing you might have to change is list($username, $email) = explode(":", $content) to list($username, $email) = explode(" : ", $content) ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277870 Share on other sites More sharing options...
nadz Posted June 19, 2007 Author Share Posted June 19, 2007 hi, thanks for the help chocopi ill just test that now. i was a bit confused - it was telling me it deleted all the addresses but they were still in the database. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277914 Share on other sites More sharing options...
nadz Posted June 19, 2007 Author Share Posted June 19, 2007 ok, im having some problems. when i use the script it echos "Deleted Email address: gggg@fgfg.com" 18000 times but it doesnt actually delete the rows in the database. heres the code im using: <?php include("config.php"); mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); $lines = file("sent.txt"); foreach($lines as $content) { list($username, $email) = explode(" : ", $content); $query = mysql_query("DELETE FROM $db_table WHERE email = '{$email}'"); if($query) { echo "Deleted email address: {$email}<br>"; } else { $error = mysql_error(); echo "Error deleting email address: {$email} - {$error}"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277947 Share on other sites More sharing options...
chocopi Posted June 19, 2007 Share Posted June 19, 2007 replace $db_table with the actual tablename, get rid of the curly braces and add an astrieks, so this line: $query = mysql_query("DELETE FROM $db_table WHERE email = '{$email}'"); looks like $query = mysql_query("DELETE * FROM tablename WHERE email='$email'"); if not replace $query = mysql_query("DELETE * FROM tablename WHERE email='$email'"); with $query = "DELETE * FROM tablename WHERE email='$email'"; mysql_query($query) or die(mysql_error()); Hope one of those works ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-277950 Share on other sites More sharing options...
nadz Posted June 19, 2007 Author Share Posted June 19, 2007 i get this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM vb1user WHERE email='n*****@googlemail.com '' at line 1 and im using this code: <?php $db_host = "mysql.t***.co.uk"; $db_user = "nextman"; $db_pwd = "*****"; $db_name = "***"; mysql_connect($db_host, $db_user, $db_pwd); mysql_select_db($db_name); $lines = file("sent.txt"); foreach($lines as $content) { list($username, $email) = explode(" : ", $content); $query = "DELETE * FROM vb1user WHERE email='$email'"; mysql_query($query) or die(mysql_error()); if($query) { echo "Deleted email address: {$email}<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/56249-deleting-rows-from-mysql-db/#findComment-278015 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.