lopes_andre Posted December 18, 2008 Share Posted December 18, 2008 Hi, I have a list of e-mails in a TXT file and I need to put that e-mails in a MySQL database. I need also to not insert duplicate e-mails. There is a best way to do this? Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/137604-solved-how-to-import-to-database-from-text-file/ Share on other sites More sharing options...
Maq Posted December 18, 2008 Share Posted December 18, 2008 *NOT TESTED Put all the emails in an array then use array_unique to rid all duplicates. Use a foreach loop to traverse through the array and insert each one in the database. $lines=file('test.txt'); foreach($lines as $line) { $arr[] = $line; } $arr = $array_unique($arr); foreach($arr as $value) { $sql = "INSERT INTO table (email) VALUES ('$value')"; } mysql_query($sql) or die(mysql_error()); P.S. - You may need to clean these variables with mysql_real_escape_string, trim stuff like that. Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/137604-solved-how-to-import-to-database-from-text-file/#findComment-719233 Share on other sites More sharing options...
fenway Posted December 19, 2008 Share Posted December 19, 2008 Or, use mysql for what databases are for... add a UNIQUE index, and use insert ignore. Quote Link to comment https://forums.phpfreaks.com/topic/137604-solved-how-to-import-to-database-from-text-file/#findComment-719664 Share on other sites More sharing options...
lopes_andre Posted December 20, 2008 Author Share Posted December 20, 2008 Thanks for your help!! Solved. I have added the UNIQUE to the email field. <?php $username="root"; $password=""; $database="emaillist_lp_pt"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $lines=file('test.txt'); foreach($lines as $line) { $arr[] = $line; $sql = "INSERT IGNORE INTO emails VALUES ('', '$line', '0', NOW())"; mysql_query($sql); } ?> I will import a file with more than 20MB. It will be any problem with the file size? Best Regards, André. Quote Link to comment https://forums.phpfreaks.com/topic/137604-solved-how-to-import-to-database-from-text-file/#findComment-720064 Share on other sites More sharing options...
Maq Posted December 20, 2008 Share Posted December 20, 2008 The file size should be fine as it is very small for DB data. I would run this script via CLI. Nice script BTW, short 'n sweet! Quote Link to comment https://forums.phpfreaks.com/topic/137604-solved-how-to-import-to-database-from-text-file/#findComment-720400 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.