The used command is not allowed with this MySQL version

守給你的承諾、 提交于 2020-01-07 06:48:08

问题


Alright, so after a week of trying all the different ideas answers I have found with no success, I am going to ask. This is for the LOAD DATA LOCAL INFILE, for MySQL. There have been many posts regarding this, but I am still unable to get it work from the web browser, but am able to run it from the mysql command prompt on the server, with the same user and pass connecting to the same database. What I have tried so far

  1. MySQL version 5.5
  2. Ubuntu 12.04
  3. PHP version 5.3

In my.cnf local-infile=1 in mysqld,mysql,mysql-safe loose-local-infile=1 client

Restarted MySQL Server. At this point I was then able to run the query from the command prompt, and previously had not. I have given the directory in which the files are being pulled from 777 access. I have confirmed the php.ini has the local file parameter enabled. I have updated apparmor. Actual Query:

LOAD DATA LOCAL INFILE '/var/www/ui/uploads/r_import.csv' INTO TABLE r_data FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS (first_name,last_name,apt,user_id)

Above query works from the mysql command with no special arguments in the connection to the server.

If anyone has anymore ideas on this, I would be happy to try anything.... Thanks in advance.

<?php
include 'includes/header.php';
if($_FILES['file']['type'] != "application/vnd.ms-excel"){
   die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
    $filename = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    copy( $filename, 'uploads/'.$name ) or die( "Could not copy file!");
    $file_to_import = '/var/www/ui/uploads/'.$name;
   $query = 'LOAD DATA LOCAL INFILE  \''.$file_to_import.'\' INTO TABLE r_data FIELDS TERMINATED BY  \',\' ENCLOSED BY  \'"\' LINES TERMINATED BY  \'\n\' IGNORE 1 ROWS (first_name,last_name,apt,user_id)';
   echo $query;
   $result = mysqli_query($link,$query) or die(mysqli_error($link));
}
else{
   die("You shouldn't be here");
}
?>

回答1:


$link = mysqli_init();
mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
mysqli_real_connect($link, 'localhost', $username, $password, $database);

The connection string to the database is what worked for me. I found it in the last comment in the link given by developerwjk




回答2:


Your problem is the use of the LOCAL keyword. When you use LOCAL the server expects the MySQL client to read the file and send it. This applies when the client software is running on a remote machine, or when you're running the MySQL client on the server itself. (This is why you can run your query from the server command line).

If you're running PHP there is no client software involved. PHP makes calls directly to the server, so the LOCAL keyword is invalid in this context.

To use LOAD DATA INFILE from PHP you must make sure that the file is placed in a location in the server filesystem that the MySQL server has read access to, and that the full path to that file is passed as part of your query. Don't use the LOCAL keyword.

If you're trying to load a file from a remote client you'll need to upload the file to the file system first, then execute your query.

Note this sentence from the MySQL manual: If LOCAL is specified, the file is read by the client program on the client host and sent to the server.

Reference: http://dev.mysql.com/doc/refman/5.5/en/load-data.html



来源:https://stackoverflow.com/questions/28550933/the-used-command-is-not-allowed-with-this-mysql-version

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