Jump to content

[SOLVED] Separate values from txt file and sotre in db


refiking

Recommended Posts

I have a list of email addresses that I want to add to my database.  In the text file they are listed like this:

 

Email Address1

Email Address 2

Email Address 3

 

I added this script:

 

$YourFile = "sql.txt";

$handle = fopen($YourFile, 'r');

$Data = fread($handle, 512);

fclose($handle);

print $Data;

 

 

And it returns like this:

 

Email Address1 Email Address2 Email Address3 ...

 

My question is how can I separate these email addresses and insert them into the db?  Here is what I've tried and it didn't work:

 

#make array from each line in the text file.
$str=explode("\n",$Data);
$values = '';
#loop through array to create the values portion of sql
foreach($str as $val)
{
    $values .= '('.$val.'), ';
}

#remove the last common.
$values = rtrim($values,',');

#run query
mysql_query('INSERT INTO emails (addy) VALUES '.$values)or die(mysql_error());

That's a pretty inefficient way of doing it. You'd be far better off sticking them in an array and imploding them. That way you can do it with just one insert statement:

 

$emails = file("sql.txt");
$emails = implode("','",$emails);
$sql = "INSERT INTO emails (addy) VALUES '$emails'";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

Edit: You ought to check for any injection attempts though:

 

$emails = file("sql.txt");
array_walk($emails,'mysql_real_escape_string');
$emails = implode("','",$emails);
$sql = "INSERT INTO emails (addy) VALUES '$emails'";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

 

 

That's a pretty inefficient way of doing it. You'd be far better off sticking them in an array and imploding them. That way you can do it with just one insert statement:

 

$emails = file("sql.txt");
$emails = implode("','",$emails);
$sql = "INSERT INTO emails (addy) VALUES '$emails'";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

correcting the above code:

 

$emails = file("sql.txt");
$emails = implode("'),('",$emails);
$sql = "INSERT INTO emails (addy) VALUES ('$emails')";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

Note the parenthesis.

 

 

That's a pretty inefficient way of doing it. You'd be far better off sticking them in an array and imploding them. That way you can do it with just one insert statement:

 

$emails = file("sql.txt");
$emails = implode("','",$emails);
$sql = "INSERT INTO emails (addy) VALUES '$emails'";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

correcting the above code:

 

$emails = file("sql.txt");
$emails = implode("'),('",$emails);
$sql = "INSERT INTO emails (addy) VALUES ('$emails')";
mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

 

Note the parenthesis.

 

Nevermind :) I see what you mean now... Where is my coffee :)

 

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.