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
importing a csv file
Moderator: General Moderators
Re: importing a csv file
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.
Use something like fgetcsv() to parse it into an array of ordered field names and build your insertion data format from there.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: importing a csv file
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
cheers for that i'll start building my app now
and i'll let you know how it goes
kev
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
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
I need to delete all the data in the table before the new data is inserted is this possible
Cheers
Kevin
Re: importing a csv file
Use mysql's TRUNCATE statement to clear out the table before importing new stuff in.