我想使用PDO和LOAD DATA INFILE將csv數據導入到mysql中(我也試過LOAD DATA LOCAL INFILE),但我一直收到一個語法錯誤,我不知道爲什麼。文件路徑和表名似乎是正確的。PHP:LOAD DATA INFILE語法錯誤
這裏是我使用的進口()函數 -
function import() {
$this->db = mydb::getConnection();
// set the column names for the selected journal table
if ($this->table = "bsp_journals") {
$columns = "category, discipline, subject, sourcetype, issn, publicationname, indabstart, indabstop, fulltextstart, fulltextstop, fulltextdelay, peerreviewed";
}
try {
$sql = "LOAD DATA LOCAL INFILE '$this->file'
INTO TABLE '$this->table'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\n'
OPTIONALLY ENCLOSED BY '\"'
($columns)";
$statement = $this->db->prepare($sql);
$statement->execute();
$this->return = $this->files['filename']['tmp_name'];
} catch (PDOException $ex) {
//throw $ex;
$this->return = $ex->getMessage() . "<br /></br />" . $sql . "<br /></br />File Name = " . $this->file;
}
return $this->return;
}
下面
是消息我得到包括MySQL錯誤消息和SQL查詢
- import result : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''bsp_journals' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ' at line 2
LOAD DATA LOCAL INFILE 'files/buh-journals.csv' INTO TABLE 'bsp_journals' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' OPTIONALLY ENCLOSED BY '"' (category, discipline, subject, sourcetype, issn, publicationname, indabstart, indabstop, fulltextstart, fulltextstop, fulltextdelay, peerreviewed)
File Name = files/buh-journals.csv
謝謝比爾。我已經試過'$ this-> table'(帶有back-ticks)和$ this-> table沒有任何引號。我仍然得到相同的錯誤,只是帶有反撥和沒有引號。 – Shane
:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到正確的語法,以便在'files/buh-journals.csv附近使用INTO TABLE bsp_journals FIELDS TERMINATED BY','第1行 LOAD DATA INFILE文件/ buh-journals.csv INTO TABLE bsp_journals FIELDS TERMINATED BY,'''線終止'''''''(類別,紀律,主題,源文件,issn,出版物名稱,indabstart,indabstop,fulltextstart,fulltextstop,fulltextdelay,peerreviewed) – Shane
我不' t似乎可以在評論中格式化,對於混亂 – Shane