Sometimes you will need to grab the content of a file and place it into your table , depending of whether your file share a delimited character structure you can easily avoid using php for make the script that parse-save your data.
Mysql comes with a pre-build command (LOAD DATA INFILE) for make your life simpler and easier , for instance let´s say that you have a csv file (that as you know it´s a simple row file coma-separated-values format) with name and email placed each one in a column, and your mysql table looks similar to:
desc tb001; | name | char(10) | YES | | NULL | | | email | char(25) | YES | | NULL | |
Well, you could load your file by using the command of above.
LOAD DATA INFILE '/data.csv' INTO TABLE db.tb001 FIELDS TERMINATED BY ',' ENCLOSED BY '""' ESCAPED BY '\\' LINES TERMINATED BY '\n';
What you´re saying is load the file /data.csv ( if is not an absolute file systempath won’t work) using the options that follows after ‘db.t001′.
One line fans:
mysql -uuser -ppassword db -e "LOAD DATA INFILE '/data.csv' INTO TABLE db.tb001 FIELDS TERMINATED BY ',' ENCLOSED BY '\"\"' ESCAPED BY '\\' LINES TERMINATED BY '\n'"
Hope this results usefull to someone.
Tags: Load data infile, Mysql load file, parse php save table, Php parse