Page 1 of 1

Reading file contents to database

Posted: Wed May 11, 2005 6:05 am
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

Posted: Wed May 11, 2005 6:55 am
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.

Posted: Wed May 11, 2005 10:35 am
by timvw
another option is reading the mysql manual. think the keyword you need is called INFILE