ThinkPHP5.x, ThinkPhp6.x如果执行语句出现错误:
✘
Cannot execute queries while other unbuffered queries are active.
例如调用Db:execute("load data local infile '/abc/def/aaa.txt' replace into table table_name fields terminated by ',' lines terminated by '\n' (field1, field2, field3)");
可能报上面的错误。
解决方法:
修改 config/database.php,找到数据库连接的params属性,修改为:
'params' => [PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_LOCAL_INFILE => true],
其中ATTR_EMULATE_PREPARES 用于解决Cannot execute queries while other unbuffered queries are active.错误, MYSQL_ATTR_LOCAL_INFILE 用于解决LOAD DATA LOCAL INFILE forbidden 错误。
如果出现errorPDOException: SQLSTATE[HY000]: General error in xxxx.php ,那么请使用 execute 替代 query。
query是用于执行必须有返回结果的查询,如果查询没有返回数据,例如create table之类,请使用execute()代替。