I found this bit of code to import a local CSV file on my PC to a MYSQL table. I saved this file as import.php.
I found the code on this page
<?php
//database connection details
$connect = mysql_connect('localhost','test','test');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
//your database name
$cid =mysql_select_db('shops',$connect);
// path where your CSV file is located
define('CSV_PATH','C:\Users\Administrator\Desktop\Images\CSV');
// Name of your CSV file
$csv_file = CSV_PATH . "Booth_shares.csv";
if (($handle = fopen($csv_file, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
$date = $col[0];
$filename = $col[1];
$directory = $col[2];
$type = $col[3];
$to = $col[4];
// SQL Query to insert data into DataBase
$query = "INSERT INTO tblshops(date,filename,directory,type,to) VALUES('".$date."','".$filename."','".$directory."','".$type."','".$to."')";
$s = mysql_query($query, $connect );
}
fclose($handle);
}
echo "File data successfully imported to database!!";
mysql_close($connect);
?>
When I navigate to example.com/import.php, it says File data successfully imported to database!! But when I log on to MYSQL, there are no records.
I verify the CSV column names match the MYSQL field names.
Any ideas?