Solved

PHP and Tab Delimited Text to MySQL

Posted on 2004-09-06
2
265 Views
Last Modified: 2013-12-12
I am working w/ PHP and MySQL. Our site allows users to enter data to be used for reports. However, much of this data can be downloaded in Tab delimited text from other sites. My question is as follows: I can parse the file and add it to our MySQL database. However the format of the text file has 255 fields. Of these, we need about thirty or less. Is there an easy way to parse and store just the imformation we need. I know the numbers of the fields we need once we get them into the array. (ie we need data[2], data[19], data[24], etc. ) Any help will be greatly appreciated and promptly rewarded.
0
Comment
Question by:chuckbeats
2 Comments
 
LVL 2

Accepted Solution

by:
benbalbo earned 500 total points
ID: 11993255
I've just had a look athe the LOAD DATA INFILE syntax and this doesn't offer the facility to skip fields.

I would proibalby opt for something alon the lines:

<?php
$fh = fopen("data.csv", "r");
while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) {
   mysql_query ('INSERT INTO `table` VALUES ('.$data[2].', '.$data[19].', '.$data[24].')');
}
fclose($fh);
?>
If you're working with huge files and this takes to long, then the other option would be to create a temporary table with all 255 fields, use the LOAD DATA INFILE syntax to populate it will all data, and then INSERT INTO `table` SELECT (col2, col19, col24) FROM `temptable`
0
 
LVL 9

Expert Comment

by:_GeG_
ID: 11993971
I think bebalbo's approach is a good way, just a few things:
<?php
// you can directly use a file from another server
$fh=fopen('http://www.example.com/tab_separated_file.txt', 'r');
$query='INSERT INTO table VALUES';
$c=0;
// 10000 must be larger than any single line you expect, change accordingly
while (($data=fgetcsv($fh, 10000, "\t"))!==FALSE){
// quote and escape every single value that you insert
   $query.=($i++?',':'')."('".mysql_escape_string(data[1])."', '".mysql_escape_string(data[5])."', '".mysql_escape_string(data[7])."', '".mysql_escape_string(data[24]).')";
}
// one single insert is much faster than a lot of small ones
mysql_query($query);
fclose($fh);
?>
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now