how to do LOAD DATA LOCAL INFILE in laravel 5.4

爷,独闯天下 提交于 2019-12-20 04:38:29

问题


i need to convert the following query in laravel 5.4

$loadDataToTempTableSql = "LOAD DATA LOCAL INFILE '".$filename."' INTO TABLE ABC FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r' IGNORE 1 LINES";
                    $loadDataToTempTableRes = mysqli_query($link,$loadDataToTempTableSql);

                    $loadedData = mysqli_affected_rows($link);

what i did

Step1:

DB::select($load_data_to_temp_table_sql);

which is throwing exception:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: LOAD DATA LOCAL INFILE '/Library/WebServer/Documents/public/abc copy.csv' INTO TABLE ABC FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ' ' IGNORE 1 LINES)

Step2:

$pdo = DB::connection()->getPdo();
$pdo->exec($load_data_to_temp_table_sql);

again exception:

PDO::exec(): LOAD DATA LOCAL INFILE forbidden

Please guide, what to do?


回答1:


I have faced same kind of problems and after googling it has been solved now. You should add the following to your mysql/my.cnf file:

[Server]
local_infile=true

After that add 'options' => array(PDO::MYSQL_ATTR_LOCAL_INFILE => true) into your project's config/database.php file.

Now restart your MySQL from cmd/terminal. It will be fine to run the LOAD DATA LOCAL INFILE. If you still face a problem, you can visit https://tenerant.com/blog/using-load-data-local-infile-in-a-laravel-migration/.




回答2:


i didn't work out with DB. i worked out with PDO and then i had to make a addition to MYSQL_ATTR_LOCAL_INFILE to the database.php file in config folder

'options' => [PDO::MYSQL_ATTR_LOCAL_INFILE => true],


来源:https://stackoverflow.com/questions/49633881/how-to-do-load-data-local-infile-in-laravel-5-4

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