dpca4790 2014-05-07 18:08
浏览 100
已采纳

为什么我不能在PDO中使用LOAD DATA LOCAL,即使我可以从cli客户端使用?

I recently upgraded my Ubuntu 10.04 dev server to 14.04. It is actually a fresh install. PHP version was 5.4.15, and is now PHP 5.5.9. MySQL went from 5.1.67 to 5.5.37.

I am trying to LOAD LOCAL DATA INFILE on a new server. It works running the command using the local mysql cli client. It does not work when executing using PDO:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version' in /home/ubuntu/mysqltest.php:21
Stack trace:
#0 /home/ubuntu/mysqltest.php(21): PDO->exec('LOAD DATA LOCAL...')
#1 {main}
  thrown in /home/ubuntu/mysqltest.php on line 21

The code that generates the error:

ini_set('mysql.allow_local_infile', 1);
echo "mysql.allow_local_infile: ".ini_get("mysql.allow_local_infile").PHP_EOL;

$options = array(PDO::MYSQL_ATTR_LOCAL_INFILE => 1);
$db = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPassword, $options);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->exec("CREATE TEMPORARY TABLE tmp_table ( id INT(4), col1 varchar(128) ) ENGINE MYISAM");

$loadSQL = <<<'EOT'
LOAD DATA LOCAL INFILE '/tmp/test.csv' INTO TABLE `tmp_table` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '
' IGNORE 1 LINES (id, col1)
EOT;

$db->exec($loadSQL);

Steps taken so far to address problem:

  • I have added local-infile = 1 to both [mysql] and [mysqld] sections of /etc/mysql/my.cnf
  • I have checked that mysql.allow_local_infile = On is in /etc/php5/cli/php.ini.
  • DB user is root and so has all privileges.
  • /tmp/test.csv does exist and user has full privileges on it.
  • 写回答

2条回答 默认 最新

  • dpq755012465 2015-02-03 20:29
    关注

    This is a bug in php. Please see https://bugs.php.net/bug.php?id=68226 for explanation and workaround.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。