Reading file contents to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheOracle
Forum Commoner
Posts: 64
Joined: Mon Nov 22, 2004 4:56 am
Location: Bedford, UK

Reading file contents to database

Post by TheOracle »

Hi All,

I want to take some data from a file (txt) and insert it into a database.

I have a rough idea how to open, read and get the contents of a basic comma delimited file, but I want to be able to read say 5 columns of data from one row and insert into 5 columns in the DB, then the same for the next row etc...

Each row should have the same number of columns, but the rows may vary.

EG

Name, value1, value2, value3
Name, value1, value2, value3

Can anybody give me a hand with this?

Many thanks,

Paul
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

pretty simple stuff on how to build your contents of the file into an array.

Code: Select all

//each line of the file is no an array
$lines = file('somefile.txt');

foreach ($lines as $new_line)
{
     //arr[0] first column
     //arr[1] second column
     //etc
     $arr = explode(',',$new_line);
}
Now you have to build your query in a loop.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

another option is reading the mysql manual. think the keyword you need is called INFILE
Post Reply