wrekanyze Posted May 15, 2007 Share Posted May 15, 2007 I have a .tab file that I need to write to the database. I've read it in, but for some reason it's not reading correctly. I'm truncating the complete database and then reloading it with whatever is in the tab file. I keep getting an error when trying to run my current code which is: <? $connection = mysql_connect('blah.blah.com', 'username', 'password'); mysql_select_db('xxx'); $fcontents = file('./textfile.TAB'); for ($i = 0; $i < sizeof($fcontents); $i++) { $line = trim($fcontents[$i], '\t'); $arr = explode("\t", $line); $sql = "insert into data values ('". implode(" ',' ", $arr) . " ')"; //$sql = "insert into data values ('". $arr ."')"; mysql_query($sql); echo $sql . "<br>\n"; if (mysql_error()) { echo mysql_error() ."<br>\n"; } } ?> Is there an easier way to read this text into the database. There's like 35 separate columns in the database in which the data is being read. Hence the need for a for loop. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/ Share on other sites More sharing options...
effigy Posted May 15, 2007 Share Posted May 15, 2007 Have you tried LOAD DATA? Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/#findComment-253850 Share on other sites More sharing options...
marf Posted May 15, 2007 Share Posted May 15, 2007 Umm, just a thought $line = trim($fcontents[$i], '\t'); $arr = explode("\t", $line); if you trim the \t, then how can you explode the \t, doesn't trimming remove all the \t's in your thing? Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/#findComment-253856 Share on other sites More sharing options...
wrekanyze Posted May 15, 2007 Author Share Posted May 15, 2007 Umm, just a thought $line = trim($fcontents[$i], '\t'); $arr = explode("\t", $line); if you trim the \t, then how can you explode the \t, doesn't trimming remove all the \t's in your thing? The \t isn't what's getting trimmed, the ftcontents variable is getting trimmed, and the t is the delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/#findComment-253860 Share on other sites More sharing options...
effigy Posted May 15, 2007 Share Posted May 15, 2007 '\t' = \t "\t" = an actual tab. Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/#findComment-253874 Share on other sites More sharing options...
wrekanyze Posted May 15, 2007 Author Share Posted May 15, 2007 '\t' = \t "\t" = an actual tab. i'm still confused. I tried that other method you gave me but it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/51550-reading-tab-delimited-file-into-database/#findComment-253908 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.