I have searched the last week for something on this, but nothing I have found works, and I have looked at the documents, and nothing in the documents work either.
I have a CSV (link below to download) that I need to upload just a couple columns to MySQL, using MySQLi. The CSV is from a platform we use to serve ads, the client name has been changed, but the data is all true data from two days.
Here is the code I have for the upload (There are 3 different versions of the SQL command, I have tried around 30 different ones):
<?
(MY USER INFORMATION FOR MY MYSQL SERVER)
$ad_results = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
ignore_user_abort(true);
set_time_limit(9999);
$sql1 = "LOAD DATA INFILE 'CSV_FILE_FOR_STACKOVERFLOW.csv' INTO TABLE ad_info
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
(@col1,@col2,@col6,@col12,@col13,@col14) set client=@col2,impressions_delivered=@col12,date=@col1,impressions_booked=@col13,clicks=@col14,creative=@col6";
$sql2 = "LOAD XML INFILE '558385_20130815-153744.xml' INTO TABLE item ROWS IDENTIFIED BY '<row>'";
$sql3 = "LOAD DATA INFILE 'CSV_FILE_FOR_STACKOVERFLOW.csv' INTO TABLE ad_info
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(@client, @name)
set
client = @client,
impressions_delivered = @impressions_delivered,
company_id = '1000001'";
mysqli_query($ad_results, $sql3);`
mysqli_close($ad_results)
?>
The MySQL database is set up with all of the columns from the CSV, just in case it is not possible to do just certain columns, and I am willing to upload all the data. The fun part is that there are 30-40 CSV files that will go through this code every day at 1AM, I have the code to select all the CSVs in the download folder, just need the code to insert them into MySQL. Thanks! (Please reply with true answers, I have searched everywhere, and nothing I have found works, it always gives me an error, or just does not insert the lines into MySQL, the closest I have gotten is parsing the CSV and using
mysqli_query($ad_results, "INSERT INTO ad_info (...)VALUES(...)");
This file lives on the server, and the cron job does run the PHP script every night, it has ignore user abort, and a run time of 9999 seconds. (to make sure it gets all the data). Thanks in advance for any help!
CSV FILE: http://ad-results.com/CSV_FILE_FOR_STACKOVERFLOW.csv
I cannot find a mysqli_error($ad_results)
call in your code. You should save the response of your mysqli_query()
in a resource-variable $mysqli
and then display a possible MySQL error like:
$mysqli=mysqli_query($ad_results, $sql3);
if (!$mysqli) echo mysqli_error($ad_results);
Maybe there is a message waiting for you after all? ;-)