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()); Link to comment https://forums.phpfreaks.com/topic/143388-solved-separate-values-from-txt-file-and-sotre-in-db/ 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]); } Link to comment https://forums.phpfreaks.com/topic/143388-solved-separate-values-from-txt-file-and-sotre-in-db/#findComment-752100 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); Link to comment https://forums.phpfreaks.com/topic/143388-solved-separate-values-from-txt-file-and-sotre-in-db/#findComment-752111 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. Link to comment https://forums.phpfreaks.com/topic/143388-solved-separate-values-from-txt-file-and-sotre-in-db/#findComment-752145 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 Link to comment https://forums.phpfreaks.com/topic/143388-solved-separate-values-from-txt-file-and-sotre-in-db/#findComment-752150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.