某些不喜欢SET NAMES utf8的主机-“在其他未缓冲的查询处于活动状态时无法执行查询." [英] Some Hosting Not Liking SET NAMES utf8 -- "Cannot execute queries while other unbuffered queries are active."

查看:109
本文介绍了某些不喜欢SET NAMES utf8的主机-“在其他未缓冲的查询处于活动状态时无法执行查询."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的连接库代码中引入了$PDO->query('SET NAMES utf8;');,以符合Unicode的要求.这适用于某些主机,在这些主机上,我可以使用.execute()使用INSERT执行PDO语句.

I've introduced $PDO->query('SET NAMES utf8;'); in my connection library code to get some Unicode compliance. This works on some hosts where I can follow this with PDO statement executions with INSERT using the .execute().

但是,在某些托管计划中,我遇到了此错误:

However, on some hosting plans, I'm encountering this error:

一般错误:2014年,其他未缓冲的实例无法执行查询 查询处于活动状态.考虑使用PDOStatement :: fetchAll(). 另外,如果您的代码只能在mysql上运行, 您可以通过设置 PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性.

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.

因此,在SET NAMES之前,我介绍了$PDO->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);,但是仍然出现错误.然后,我将该值设置为FALSE,但仍然出现错误.

So, before SET NAMES, I introduced $PDO->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE);, but I'm still getting the error. I then set that value to FALSE and I'm still getting the error.

当然,如果我删除了"SET NAMES utf8"语句,问题就消失了.再说一次-此问题仅在某些Web主机上发生.

Of course, if I remove the "SET NAMES utf8" statement, the problem goes away. And again -- this problem only occurs on some web hosts.

将MySQL转换为适用于大多数Linux托管计划的Unicode的正确方法是什么?

What's the proper way to switch MySQL to Unicode that works on most Linux hosting plans?

推荐答案

将连接编码设置为UTF-8的首选方法是在PDO构造函数中使用PDO::MYSQL_ATTR_INIT_COMMAND,如

The preferred way to set the connection encoding to UTF-8 is the use of PDO::MYSQL_ATTR_INIT_COMMAND in the PDO constructor, as shown in the manual:

$dsn      = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options  = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

$dbh = new PDO($dsn, $username, $password, $options);

这应该一直有效.

这篇关于某些不喜欢SET NAMES utf8的主机-“在其他未缓冲的查询处于活动状态时无法执行查询."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆