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());

Link to comment
Share on other sites

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);

 

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 :)

 

Link to comment
Share on other sites

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.