PDO :: MYSQL_ATTR_INIT_COMMAND似乎打破了查询,尝试自己的。
以下内容:
$connection = new \PDO("mysql:host=localhost;dbname=php_orm_test", "dev", "dev",[
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8; SET CHARACTER SET UTF8; SET character_set_connection=UTF8; SET character_set_client=UTF8;"
]);
$statement = $connection->prepare("describe users");
$statement->execute();
$statement->fetchAll(\PDO::FETCH_ASSOC);
var_dump($statement->errorInfo());
$statement->closeCursor();
抛出:
array(3) {
[0]=>
string(5) "00000"
[1]=>
int(2014)
[2]=>
string(269) "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."
}
如果我删除了MYSQL_ATTR_INIT_COMMAND,一切都运行正常......任何想法?
请注意,即使这样也行不通......
PDO::MYSQL_ATTR_INIT_COMMAND => self::DRIVER_INIT,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
答案 0 :(得分:6)
您使用的是PHP> = 5.3.6,因此请将字符集添加到dsn,不要使用PDO::MYSQL_ATTR_INIT_COMMAND
。此外,我认为您只能使用PDO::MYSQL_ATTR_INIT_COMMAND
执行1个查询。这可能是问题所在。
mysql:host=localhost;dbname=php_orm_test;charset=utf8
答案 1 :(得分:5)
您至少可以使用逗号分隔的多个SET
语句,例如SET names utf8, @@session.sql_mode = '';