I\'ve got a CSV file with 9 columns and I have a MySQL table with 11 columns.
The CSV file looks like:
col1, col2, col3, col4, col5, col6, col7, col8
Looks like you can use LINES TERMINATED BY
(perhaps with PHP_EOL
).
http://dev.mysql.com/doc/refman/5.0/en/load-data.html
To quote the manual page:
If a line does not contain all fields, the rest of the columns are set to their default values.
$sql = 'LOAD DATA LOCAL INFILE "../csvtemp/test.csv"
INTO TABLE sample
FIELDS TERMINATED BY ","
OPTIONALLY ENCLOSED BY """"
IGNORE 1 LINES
(col1, col2, col3, col4, col5, col6, col7, col8, col9)'
;
The missing columns will be given their DEFAULT values, or else you can specify fixed values this way:
$sql = 'LOAD DATA LOCAL INFILE "../csvtemp/test.csv"
INTO TABLE sample
FIELDS TERMINATED BY ","
OPTIONALLY ENCLOSED BY """"
IGNORE 1 LINES
(col1, col2, col3, col4, col5, col6, col7, col8, col9)'
SET col10 = 'abc', col11 = 'xyz'
;