I have a .csv file named 'result.csv'. it contains:
1,2,3,4,6,5,7
I want to import the .csv file to my_table in SQL (I want to put the .csv value to 'id_2' column) with this code, but this code doesn't work properly.
<?php
$host = "localhost";
$db_name = "tsp";
$username = "root";
$password = "";
$conn = new PDO("mysql:host=" .$host . ";dbname=" . $db_name, $username, $password);
define('CSV_PATH','C:/Users/user/Downloads/'); // CSV file path
$csv_file = CSV_PATH . "result.csv"; // Name of CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['id_2'] = $csv_array[0];
$sql = $conn->prepare("INSERT INTO my_table(id_2) VALUES('','".$insert_csv['id_2'].")");
$sql->execute();
$i++;
}
fclose($csvfile);
?>
What should I do? Thanks for your help