Perfidus Posted November 21, 2007 Share Posted November 21, 2007 Hi there, I have a huge TXT with a list of streets: street 1 street 2 street 3 I would like the php to read them and place them in the same order as they appear in the database inside the street column. I know how to do it when txt looks like street 1, street 2, street 3 but don't know how to handle with the line breaks. Any hints? Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/ Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 <?php $lines = file('streetsfile.txt'); foreach ($lines as $line) { $sql = "INSERT INTO tbl (street) VALUES ('$line'); // execute query. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-395948 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Thank you Thorpe, I've been trying this code but only the last name in the list is stored in the database. The 3000 before this one have been ignored, why could this be? Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396035 Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396040 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Uppps, I forgot to include the execution inside the bucle !!! By the way, can I read two TXT simultaneously to add the ZIP's to the database, cause I have another list with all the zip codes in the same order as streets are... I guess this is SCIFI, isn't it? <?php $lines = file('streetsfile.txt'); $lines2 = file('zipsfile.txt'); foreach (($lines as $line)&&($lines2 as $line2)) { $sql = "INSERT INTO tbl (street, zip) VALUES ('$line', '$line2'); // execute query. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396052 Share on other sites More sharing options...
obsidian Posted November 21, 2007 Share Posted November 21, 2007 Your foreach cannot be done that way. Try this instead: <?php foreach ($lines as $n => $line) { $sql = "INSERT INTO tbl (street, zip) VALUES ('$line', '" . $lines2[$n] . "')"; // execute query. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396069 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Using first code I'm getting a 1064 error, cause some street names are repeated (there are streets that have 1 or 2 zip codes each) how can I allow repetition? Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396075 Share on other sites More sharing options...
obsidian Posted November 21, 2007 Share Posted November 21, 2007 Using first code I'm getting a 1064 error, cause some street names are repeated (there are streets that have 1 or 2 zip codes each) how can I allow repetition? Set up your database to not have a UNIQUE key on those columns. Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396078 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Yeah, I have already checked this out, but let me see again... Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396081 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Ok, the problem doesn't come from there but from the fact that some streets look like this: D'Artagnang This a apostrophe is killing the thing... Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396085 Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 You need to escape the data then. Take a look at mysql_real_escape_string. Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396088 Share on other sites More sharing options...
Perfidus Posted November 21, 2007 Author Share Posted November 21, 2007 Yeah, I have already fixed that by using \' Thank u very much for your help Thorpe !!!! Now data is uploaded and everything working really fine. Quote Link to comment https://forums.phpfreaks.com/topic/78244-solved-reading-a-txt/#findComment-396091 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.