Solved

PHP and Tab Delimited Text to MySQL

Posted on 2004-09-06
2
270 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

730 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