I'm trying to automate the import of a CSV file into MySQL. I thought I could run a PHP file as a cronjob on my server as PHPMyAdmin doesn't have Events enabled (and can't be due to webhost).
I've been trying with LOAD DATA Syntax as seems popular when I google the issue and come across the fifty million threads on Stack Overflow. However, I'm having trouble debugging my own configuration and frustrated that I can't find a way for it to show me errors to help work out what I'm doing wrong.
I know my CSV file variable is correct as I have a code to check that and it passes (I've also purposely made it fail and it fails, so definitely working):
$file = fopen($csvfile,"r");
if (!$file) {
echo "<h1>bachHa'!</h1><p>Error opening data file.</p>\n";
exit;
}
So, I know I've located my CSV file and I'm passing that correctly to LOAD DATA, but after that, who knows, all I know is I don't get anything in my database after it runs:
$import = <<<eof
LOAD DATA LOCAL INFILE '$csvfile'
INTO TABLE test_upload
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(response_id,date_submitted,status,q1,q5_text,q2,q3,q4_1,q4_2,q4_3,q4_4,q4_5,StoreName);
eof;
$conn->query($import);
The column headers are accurate, my datafile is aligned with them, empty columns are allowed NULL.
I've looked through the manual but can't find a way to grab the errors to display in my PHP file, echo'ing $import looks fine and $conn doesn't echo anything (not sure if it should, I'm not a programmer and just trying to bodge my way through this).
Example of the csv file:
"Response ID","Date Submitted","Status","Q01","Q05","Q02","Q03","Q04_01","Q04_02","Q04_03","Q04_04","Q04_05","StoreName"
"62","2019/05/06 16:43:59","Complete","8","","8","No","Very good","Good","Very good","Good","Very good","Store A"
"63","2019/05/06 16:45:36","Complete","10","I had a very nice experience with a lady called Karen she was very helpful","10","Yes","Very good","Good","Good","Good","Very good","Store C"
Any help is greatly received!
$conn = new mysqli($servername, $username, $password, $dbname);- will try a PDO, not done one of those before.