使用php LOAD DATA INFILE将CSV文件导入MySQL表

时间:2017-08-03 08:34:52

标签: php mysql csv

我正在尝试将CS​​V文件导入MySQL表。我的代码是

$sql1 = "LOAD DATA INFILE '$this->filePath' INTO TABLE xyz FIELDS TERMINATED BY ',' ENCLOSED BY '\' LINES TERMINATED BY '\n' IGNORE 1 ROWS";
mysqli_query($this->conn, $sql1) or die("Problem:". mysqli_error($this->conn));

执行代码时,脚本会给我一个错误,如

  

“您的SQL语法有错误;请查看手册   对应于您的MariaDB服务器版本,以获得正确的语法   靠近''IGNORE 1 ROWS'第2行“

1 个答案:

答案 0 :(得分:0)

您需要转义\。试试这个

$sql1 = "LOAD DATA INFILE '$this->filePath' INTO TABLE xyz FIELDS TERMINATED BY ',' ENCLOSED BY '\\' LINES TERMINATED BY '\\n' IGNORE 1 LINES";