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. 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? 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? 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. 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. 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. 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
Archived
This topic is now archived and is closed to further replies.