Programming and Webmasters forum
HomeSearchRecent PostsLoginRegister Contact Us

Username  
Password
Announcing 14th Weekly Contest - From 25 July To 01 August.

Win every week on this forum.

Chek out How To Win?
 

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
Contest Points: 100
 
New Coder
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!";


?>

Retractable Reels | Anchor Hocking Glass
 
webmaster forum
Admin  Offline
*
 
Hacker
Location: India
Gender: Male
Posts: 1101
Topics: 94
Technical_Talk
WWW
February 17, 2010, 07:26:54 AM

What error or issue you are facing while running this code?

Watch out for the latest Weekly Contests | Contest Rules
A Game - Say "Hello"
We are looking for Global Moderator
 
  Email this topic  |  Print
Pages: [1]   Go Up
 
Jump to:  



Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC


Google visited last this page July 30, 2010, 05:12:43 AM

Valid XHTML 1.0 Transitional     Valid XHTML 1.0 Transitional