importing a csv file

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

importing a csv file

Post by kevrelland »

hi
i know it is possible to take a csv file and import it to a mysql db via php
but my problem is that my order of data does not match the order of the fields of the db
i know i can do this with navicat, but i wanted to build an online solution

So what i want to know is, can i set the order that the data is imported in to the db with php?
and if you could give me some pointers or direction on where to look.

Cheers
Kevin
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: importing a csv file

Post by Eran »

The thing you need to be concerned with is matching the CSV field names to the field names of your db table. You can get the structure of all the rows from the first non-empty row in the CSV file which should contain the field names.
Use something like fgetcsv() to parse it into an array of ordered field names and build your insertion data format from there.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: importing a csv file

Post by Christopher »

You can specify the order of the columns in both the INSERT and LOAD DATA INFILE statements.
(#10850)
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

Re: importing a csv file

Post by kevrelland »

cheers for that i'll start building my app now
and i'll let you know how it goes
kev
kevrelland
Forum Commoner
Posts: 73
Joined: Mon Jan 08, 2007 7:41 am

Re: importing a csv file

Post by kevrelland »

i have a new question in relation to this problem,

I need to delete all the data in the table before the new data is inserted is this possible
Cheers
Kevin
LBmtb
Forum Newbie
Posts: 23
Joined: Wed May 14, 2008 11:14 am

Re: importing a csv file

Post by LBmtb »

Use mysql's TRUNCATE statement to clear out the table before importing new stuff in.
Post Reply