Programming, website development forum Get latest updates by RSS Follow TechnicalTalk on Twitter Follow TechnicalTalk on Facebook 
HomeSearchRecent PostsLoginRegisterContact Us

Username  
Password    
  Forgot your password?  

Pages: [1]   Go Down
 
  Email this topic  |  Print
0 Members and 1 Guest are viewing this topic.

Parse Tab Delimited txt file into MySql

 
webmaster forum
giclee12  Offline
Activity
0%
 
New Poster
Posts: 10
Topics: 3
February 17, 2010, 03:18:52 AM

I have a Tab delimited txt file that I need to import into a mysql database, however I am having an issue in that I dont think there is an end of line character within the file.

The code below is something i have picked up via google and made a couple of amends to, can someone please look at this and see if they can see anything obviously wrong with it?

The txt file itself is in the same directory at the mintue as the php file and when opened with either open office word or spreadsheet doesnt contain any | between fields, just tabs.

I would include a link to the txt file however it is 75MB.

Thanks

Quote
<?php

// Set Mysql Variables
//$host = 'localhost';
//$user = 'root';
//$pass = 'ASsC9XlI';
//$db = 'testdb';
$table = 'on_market_dump';

$username = "user";
$pass = 'mypass';
$db = mysql_connect("localhost", $username, $pass);
mysql_select_db("testdb",$db);

//mysql_connect($host,$user,$pass) or die(mysql_error());
$empty_table = "TRUNCATE TABLE '$table'";
mysql_query($empty_table) or die (mysql_error());

$file = "on_market. export_urls_rich. txt";
$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);

//$output = str_replace("\t|\t", "|", $data);

$output = explode("\n", $output);

//mysql_connect($host,$user,$pass) or die(mysql_error());

foreach($output as $var) {
$tmp = explode("\t", $var);
$productid = $tmp[0];
$prodid = $tmp[1];
$quality = $tmp[2];
$url_spec = $tmp[3];
$supplier_id = $tmp[4];
$highres = $tmp[5];
$lowres = $tmp[6];
$thumbnail = $tmp[7];
$uncatid = $tmp[8];
$catid = $tmp[9];
$manu_pn = $tmp[10];
$ean_upcs = $tmp[11];
$modelname = $tmp[12];
$onmarket = $tmp[15];
$countries = $tmp[16];
$updated = $tmp[17];


$sql = "INSERT INTO $table SET productid='$productid', prodid='$prodid', quality='$quality'";
mysql_query($sql)or die (mysql_error());

}
echo "Done!";


?>
 
webmaster forum
Admin  Offline
*
 
Code Guru
Location: India
Gender: Male
Posts: 1387
Topics: 105
NaviBuster NaviBuster
WWW
February 17, 2010, 07:26:54 AM

What error or issue you are facing while running this code?
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.15 | SMF © 2011, Simple Machines


Google visited last this page February 06, 2012, 06:05:27 AM