Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.