LOAD DATA LOCAL INFILE does not work from php 5.5 using PDO

ぐ巨炮叔叔 提交于 2019-12-29 06:54:31

问题


I've recently moved web servers, and the new web server has a different version of PHP.

New Server: PHP 5.5.3-1ubuntu2 (cli) Old Server: PHP 5.3.10 (cli)

On the database server, my.cnf is set to allow local-infile. I can use load data local infile from:

- HeidiSQL
- The command line client running on the new web server using --local-infile=1
- PHP, using PDO, from the old web server

However, when I try to connect from the new web server, using PHP with PDO, I get: A database problem has occurred: SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version

php.ini:

[MySQL]
; Allow accessing, from PHP's perspective, local files with LOAD DATA statements
; http://php.net/mysql.allow_local_infile
mysql.allow_local_infile = On

PDO constructor:

$this->db_conn = new PDO("mysql:host=$host;dbname=$dbname;port=$port", $user, $pass, array(PDO::MYSQL_ATTR_LOCAL_INFILE => 1));

I've also tried ini_set('mysql.allow_local_infile', 1) and ini_set('mysql.allow_local_infile', true) in the PHP script.

The LOCAL keyword is needed because the files are hosted on the web server, not the database server.

What else does PHP 5.5 want from me?


回答1:


You need to configure both the client and the server to allow this command:

  1. Make sure the server allows LOAD DATA LOCAL INFILE. Edit /etc/my.cnf on the MySQL server:

    [server]
    local-infile=1
    
  2. Set the PDO attribute in your PHP script:

    <?php
    
    $dsn = "mysql:host=localhost;dbname=test";
    $user = "root";
    $password = "root";
    $pdo = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_LOCAL_INFILE=>1));
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $pdo->exec("LOAD DATA LOCAL INFILE 'foo.csv' INTO TABLE foo FIELDS TERMINATED BY ','");
    

    This needs to be set in the driver options argument to the constructor, not in a subsequent call to setAttribute().

    I just tested the above code example successfully with PHP 5.5.8 and Percona Server 5.6.15 on CentOS Linux 6.5.

If you still have trouble, you may have a build of the MySQL client or PDO that does not permit local-infile. The requirements are described in http://dev.mysql.com/doc/refman/5.6/en/load-data-local.html

The mysql.allow_local_infile option in your php.ini is not relevant to PDO.




回答2:


Your code is absolutelty correct no need to change as the error lies in your database file.

error cause

The reason of your error caused as you have created the database backup through commandline and trying to upload data through custom php script.

solution

There are 2 solutions available to resolved the issue are:-

  1. Try to load database through commandline and if you dont have access to commandline then you can ask your service provider to do upload for you. As he has to just run simple command.

  2. Open the databse file in text editor and check the headers which encoding is used. Then read the data and de-encode it then run the insert command.

If the problem doesnt resolve then let me know..



来源:https://stackoverflow.com/questions/20226631/load-data-local-infile-does-not-work-from-php-5-5-using-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!