欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PDO导入CSV,”LOAD DATA LOCAL INFILE”报错解决

程序员文章站 2022-05-11 18:20:46
...

本地wamp导入csv无报错代码: LOAD DATA INFILE '/opt/htdocs/www/xxx.com/xxx/public/upload/53a7d83f00e08b4f0121d80f.csv' REPLACE INTO TABLE newcardCHARACTER SET utf8FIELDS TERMINATED BY '' ENCLOSED BY '\"'LINES TERMINATED BY '\r\n' (`code`) 传

本地wamp导入csv无报错代码:

LOAD DATA INFILE  '/opt/htdocs/www/xxx.com/xxx/public/upload/53a7d83f00e08b4f0121d80f.csv' REPLACE INTO  TABLE newcard
CHARACTER SET utf8
FIELDS TERMINATED BY '' ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n' (`code`)

传到服务器上的时候报

Access denied.

后来查了下原因是由于数据库服务器和文件服务器不在同一台机器上需要使用”LOAD DATA LOCAL INFILE”.

于是将代码改成

LOAD DATA LOCAL INFILE  '/opt/htdocs/www/xxx.com/xxx/public/upload/53a7d83f00e08b4f0121d80f.csv' REPLACE INTO  TABLE newcard
CHARACTER SET utf8
FIELDS TERMINATED BY '' ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n' (`code`)

Warning: PDO::exec(): LOAD DATA LOCAL INFILE forbidden in /opt/htdocs/www/xxx.com/xxx/public/test.php on line 12

后来google了一下,在load-data-local-infile-forbidden-in-php有解决方案

在pdo初始化的时候传入以下参数即可解决问题

\PDO::MYSQL_ATTR_LOCAL_INFILE => true

转载请注明:Jesus.Pan的博客 » PDO导入CSV,”LOAD DATA LOCAL INFILE”报错解决

PDO导入CSV,”LOAD DATA LOCAL INFILE”报错解决,首发于Jesus.Pan的博客。