?
Solved

PHP and Tab Delimited Text to MySQL

Posted on 2004-09-06
2
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
benbalbo earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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