Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP and Tab Delimited Text to MySQL

Posted on 2004-09-06
2
Medium Priority
?
275 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 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 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