Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP and Tab Delimited Text to MySQL

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 …
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…
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 …
Suggested Courses

610 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