I need to write a PHP script that takes CSV files with 166 columns (yes, 166) and around 20.000 rows. I have no decision regarding the csv files. On top of that, they arent regular csv files. For some reason the delimiter in these files is ";" and not a colon. Some fields are completely empty which in the csv is represented like this: value1;;value3.
I already have the Database and table with matching columns, but with an additional id column as 1st column.
What I've tried:
Getting a LOAD DATA (LOCAL) INFILE query working, but after 15 hours of relentless trying, I decided to ask here. The production server I'm working on is a regular LAMP stack in UBUNTU server but also with a local XAMPP installation it didn't work at all.
I've also tried loading the csv into an array and then looping through it but the csv was never trimmed right.
Even if the LOAD DATA INFILE would work, I'd still have the problem that the 1 column in the DB is set to auto_increment and I really don't want to specify 166 column headers in the sql query.
MySQL:
LOAD DATA INFILE 'D:/User Directories/Desktop/test.csv'
INTO TABLE nwcatalogue
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
PHP:
//I don't know ho to specify the delimiter for str_getcsv when used as
//callback
$csv = array_map('str_getcsv', file('data.csv'));
The LOAD DATA INFILE sometimes runs without error, but no records are inserted. Sometimes I get a permission denied error and most often I get "duplicate keys" or "out of range" errors. The query works neither with a Windows nor with a linux file system.
The PHP str_getcsv function works (a bit) better but the values are sometimes not seperated in the right spot. e.G I get values in the array like this:
Array =>
[0] => 0;1;;;2;1;2;1
[1] => 0;4;;12;1;0;5
[2] => 5;1;1;;;1;2;;
[3] => 4;1;;;2;1;2;7
If there is no value between 2 semicolons, it should just insert NULL as the value.
In short, I either need an array where each value has a specific key so that I can loop through it easily and row by row or i need to get this LOAD DATA INFILE query working.
I also have admin rights on the web server and mysql server.