문제

Anyone that is aware of the issues with using LOCAL INFILE in PHP will understand my frustration with this.

I previously got LOCAL INFILE working properly in all my PHP scripts by using the 128 flag in my connection call.

mysql_connect(DB_SERVER,DB_USER,DB_PASS,false,128)

I am now trying to switch to mysqli since the old mysql is depreciated. I have tried everything I could find on every forum out there with no luck.

My connection call in mysqli seems fine:

$connection = mysqli_init();
if (!$connection) {die('mysqli_init failed');}
if (!mysqli_options($connection, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {die('Setting MYSQLI_INIT_COMMAND failed');}
if (!mysqli_options($connection, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');}
if (!mysqli_options($connection, MYSQLI_OPT_LOCAL_INFILE,true)) {die('Setting MYSQLI_OPT_LOCAL_INFILE failed');}

if (!mysqli_real_connect($connection, DB_SERVER, DB_USER, DB_PASS, DB_SELECT)) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

My my.cnf file contains the appropriate parameters:

[mysql]
local-infile=1

[mysqld]
local-infile=1

my php.ini also contains the appropriate parameters:

[MySQLi]
mysqli.allow_local_infile = On

[MySQL]
mysql.allow_local_infile = On

The query even returns true when executed! But i still get no data loaded in my table.

$query = "LOAD DATA LOCAL INFILE '{$file}' INTO TABLE {$table} FIELDS TERMINATED BY '{$fieldTerm}' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '{$lineTerm}' IGNORE {$skipLines} LINES";
if(mysqli_query($connection,$query)) {$message .= "Load Complete <br />";} //Message returns with "Load Complete".

Other than doing a system() call, I have no idea what to do to make this work. It worked fine with mysql, just not now with mysqli.

Why is LOCAL INFILE so difficult? This has been an ongoing problem for way too long.

EDIT:

Strange, when I run mysqli_info() after the query, it says it executed properly.

Records: 4 Deleted: 0 Skipped: 0 Warnings: 0

There are 4 records in the test file, so this is correct.

도움이 되었습니까?

해결책

A quick google search brought up the following PHP Bug entry: https://bugs.php.net/bug.php?id=55737.

This issue seems to revolve around the php.ini setting open_basedir: http://www.php.net/manual/en/ini.core.php#ini.open-basedir

The LOAD DATA LOCAL INFILE function does not work with mysql5.5 and php5.4.4 on Debian Wheezy with open_basedir restrictions in place. It works perfectly fine when open_basedir is disabled or set to nothing.

EDIT (solution for reference): OP's issue was with the "SET AUTOCOMMIT = 0" init command. It was loading and processing, but not being committed to the db.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top