如果我在mysql workbench上运行以下sql,那么一切正常,但只要我尝试通过php调用它:
LOAD DATA INFILE 'C://stats/imports/All Members/ALL_MEMBERS_(ACTIVE)_30_09_2018.csv'
INTO TABLE stats.members_test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\\\\'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(mem_no, @entered_on, @date_of_joining, mem_status, @mem_status_date, pitch_code, branch_code, section_code, sub_section_code, mem_sector, emp_status, employer, homeworker, workplace, personal_contract, pay_method, @date_paid_upto, home_country, work_country, equal_opps_consent, mem_ben_consent, priv_agree_consent, app_method, age, gender)
SET date_of_joining = str_to_date(@date_of_joining,'%d-%b-%Y'),
entered_on = str_to_date(@entered_on,'%d-%b-%Y'),
mem_status_date = str_to_date(@mem_status_date,'%d-%b-%Y'),
date_paid_upto = str_to_date(@date_paid_upto,'%d-%b-%Y')
... 我收到一条错误消息,说明:
sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取在“”附近使用的正确语法(mem\u no,@entered\u on,@date\u of \u joining,mem\u status,@mem\u s'位于第6行)
web应用程序、数据库和csv文件都在同一台服务器上。
php代码是:
function loadData() {
try
{
$db = DAO::ConnectToDB();
$sql = "LOAD DATA INFILE 'C://stats/imports/All Members/ALL_MEMBERS_(ACTIVE)_30_09_2018.csv'
INTO TABLE stats.members_test
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
(mem_no, @entered_on, @date_of_joining, mem_status, @mem_status_date, pitch_code, branch_code, section_code, sub_section_code, mem_sector, emp_status, employer, homeworker, workplace, personal_contract, pay_method, @date_paid_upto, home_country, work_country, equal_opps_consent, mem_ben_consent, priv_agree_consent, app_method, age, gender)
SET date_of_joining = str_to_date(@date_of_joining,'%d-%b-%Y'),
entered_on = str_to_date(@entered_on,'%d-%b-%Y'),
mem_status_date = str_to_date(@mem_status_date,'%d-%b-%Y'),
date_paid_upto = str_to_date(@date_paid_upto,'%d-%b-%Y')";
$db->exec($sql);
}
catch(PDOException $e)
{
$myError = $e->getMessage();
return $myError;
}
问题已解决。必须添加额外的转义字符以转义为和以结尾的行。上面的代码现在起作用了。
暂无答案!
目前还没有任何答案,快来回答吧!