refiking Posted January 30, 2009 Share Posted January 30, 2009 I'm sure this is simple, but I'm struggling with finding out exactly how to do this. I have a list of names that I want to add to my database. In the text file they are listed like this: Name1 Name2 Name3 I added this script: $YourFile = "sql.txt"; $handle = fopen($YourFile, 'r'); $Data = fread($handle, 512); fclose($handle); print $Data; And it returns like this: Name1 Name2 Name3 Name4 My question is how can I code the script so that everytime there is a space, it adds a record to the db? Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/ Share on other sites More sharing options...
lonewolf217 Posted January 30, 2009 Share Posted January 30, 2009 if the format is always going to be like that, import the entire file into the variable, then run <?php $names = explode(" ",$Data); foreach($names as $name) { //insert into database } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-750732 Share on other sites More sharing options...
phparray Posted January 30, 2009 Share Posted January 30, 2009 lonewolf217 he said the data was in a text file on new lines. It displaying in a browser as spaces because browsers don't use the \n character. This should work. $YourFile = "sql.txt"; $handle = fopen($YourFile, 'r'); $Data = fread($handle, 512); fclose($handle); #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 table (columnName) VALUES '.$values); Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-750736 Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 To do a cleaner version without the fopen junk, I prefer this method: $YourFile = "sql.txt"; $Data = file($YourFile); // if it gives you a sql error uncomment this line: // array_pop($Data); #remove the last common. $values = implode("' , '", $Data); #run query mysql_query('INSERT INTO table (columnName) VALUES ('.$values . ')'); At least I think that's a bit cleaner Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-750742 Share on other sites More sharing options...
phparray Posted January 30, 2009 Share Posted January 30, 2009 The use of implode() is a great idea! I can't believe I've never thought of that. Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-750753 Share on other sites More sharing options...
premiso Posted January 30, 2009 Share Posted January 30, 2009 The use of implode() is a great idea! I can't believe I've never thought of that. I was the same way when I was shown the light. Glad you could learn something new Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-750770 Share on other sites More sharing options...
refiking Posted February 1, 2009 Author Share Posted February 1, 2009 Some of these are not names, but email addresses as well. I get this error message when trying to execute this script: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@sbcglobal.net ), (Randall ), (Micheal ), (rtlite@jlw.net' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-751789 Share on other sites More sharing options...
phparray Posted February 3, 2009 Share Posted February 3, 2009 change mysql_query('INSERT INTO table (columnName) VALUES ('.$values . ')'); to mysql_query('INSERT INTO table (columnName) VALUES ("'.$values . '")'); Quote Link to comment https://forums.phpfreaks.com/topic/143146-how-can-i-store-values-on-db/#findComment-753066 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.