问题描述
我在我的连接库代码中引入了$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:
因此,在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的主机-“在其他未缓冲的查询处于活动状态时无法执行查询."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!