View Full Version : NEED a php script to import .CSV into mysql table!! HELP PLEASE!
I need to make a php script that will take .csv file from the folder on my hosting and insert all data from this file into mysql table. I have a file on server in the folder htdocs/db/file.csv.
I tried to use:
mysql_query("load data infile './/db//file.csv' into table tablename fields terminated by ',' enclosed by '\"' lines terminated by '\r\n'");
but it doesn't work.
I need it very much! Can anyone write me an example. Using LOAD DATA INFILE. Please explain to me, so I can understand!
mitchind
9-26-05, 06:07 PM
Why are you starting a new thread and not reading replies to your last one?
I'm reading old replies, thank you for that. But I need to have php script, and I need it rather urgent. I need to do it using LOAD DATA INFILE.
mitchind
9-26-05, 07:04 PM
If you need it so urgently, perhaps you should actually read my suggestions in your last thread.
As far as I can tell there should be no real reason why you need to use Load Data Infile. Download the file to your PC, and use phpMyAdmin - instructions in your other thread.
If you must use php for some reason????, use mysqlimport from exec, system or passthru PHP functions. Link to the section in the manual also in the other thread
I can't download it to my PC and use phpMyAdmin because I need to make a php script that should run by sheduler, and if that file was updated, automatically update it in the base (delete all prev lines, and insert file.csv).
I read that manual in prev thread, I don't understand how to make it. Could you please write and example? How to use mysqlimport from exec?
Is using of Load Data Infile a bad way to do it? Why?
mitchind
9-27-05, 12:27 AM
I think it would help you greatly if you spent a little up-front time searching previous posts and reading the manual. If you're going to write this script in PHP you'll need to understand how to insert variables in strings - that's basic PHP - and pass a string to a system command.
As I mentioned before, the documentation for the mysqlimport command was in the other thread.
There's many examples in other threads here - including sample code for tab-delimited files. But for your sake here's a start ...
The command line for a tab-delimited file is:
/usr/local/bin/mysqlimport -v -L --low-priority -u<username> -p<password> -h<mysql##>.powweb.com <databasename> <tablename>.extension
If you're running it on your own PC, take out the -L flag
If you want to empty the contents of the table first, add the -d flag.
The parameters for using different delimiters are in the manual. With this you can try some coding yourself first - then come back here for help. If you need it done immediately and don't want to try it out yourself, I'm sure someone here can write the code for you for a nominal fee.
As for LOAD DATA INFILE - I'm almost positive Powweb doesn't grant anybody FILE privilege, so you won't have much luck getting it going here, unless something's changed with the last upgrade.
Thank you very much, mitchind!
I know how to insert variables in strings:)
I guess I couldn't do it because of FILE privilege, I think you are right, PW doesn't grant it.
I'll try to use command line. If any questions, I'll ask here)
Thanks!
vBulletin v3.6.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.