refiking Posted February 1, 2009 Share Posted February 1, 2009 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()); Quote Link to comment Share on other sites More sharing options...
tivrfoa Posted February 1, 2009 Share Posted February 1, 2009 read each line of the file and save in an array eg: emails = array(); emails[] = line; then you insert in your table for($i = 0..) { $sql = insert into ... (emails[$i]); } Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted February 1, 2009 Share Posted February 1, 2009 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); Quote Link to comment Share on other sites More sharing options...
tivrfoa Posted February 1, 2009 Share Posted February 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
MatthewJ Posted February 1, 2009 Share Posted February 1, 2009 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 Quote Link to comment 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.