Jump to content

Reading Tab Delimited File into Database


wrekanyze

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.