Jump to content

[SOLVED] How to import to database from text file?


lopes_andre

Recommended Posts

*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!

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é.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.