Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.