我試圖使用LOAD DATA LOCAL INFILE
從另一個服務器導入csv文件,但它給了我一個錯誤The user update failed: Can't find file
,即使文件存在於給定的路徑中。我也試過LOAD DATA INFILE
但在PHP中將CSV文件導入到Mysql數據庫中
是否有任何其他方法直接導入CSV文件的文件路徑(不使用任何提交按鈕),dBASE表在MySQL數據庫中,並刪除當前數據,如果有一個新的csv文件上傳?
編輯:我也試過這個PHP腳本,導入CSV文件無任何按鍵,但是csv文件數據無法導入
PHP(第2碼):
$mysqli = new mysqli("localhost", "user", "password", "dbasename");
$file = "/var/www/html/files/filename.csv";
$handle = fopen($file,'r');
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE){
$num = count($data);
for ($c=0; $c < $num; $c++){
$col[$c] = $data[$c];
}
$col1 = $data[0];
$col2 = $data[1];
$col3 = $data[2];
$col4 = $data[3];
$col5 = $data[4];
// SQL Query to insert data into DataBase
$query = "INSERT INTO dbname.tablename(col1,col2,col3,col4,col5) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."')";
$mysqli->query($query);
}
fclose($handle);
這裏的的PHP:
$sql = "LOAD DATA LOCAL INFILE 'filename.csv'
INTO TABLE dbname.tablename
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;";
$con=mysqli_connect("localhost","user","password","db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
};
$result = mysqli_query($con, $sql);
if (mysqli_affected_rows($con) == 1){
$message = "The data was successfully added!";
} else {
$message = "The user update failed: ";
$message .= mysqli_error($con);
};
echo $message;
@ e4c5,我並不想導出數據,但將其導入到MySQL數據庫。我不這是重複的 – User014019