我的数据库中的表是使用正确的UTF-8字符集创建的,类似于这样:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
...
...
...
...
...
PRIMARY KEY (id)
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_slovak_ci;
然而,当我使用Zend_Db_Table使用此方法从表中获取数据时:
public function getSingle($id)
{
$select = $this->select();
$where = $this->getAdapter()->quoteInto( id = ? , $id, INTEGER );
$select->where($where);
return $this->fetchRow($select);
}
它返回一个带有破损UTF-8字符的对象(我猜转换为iso-8859-1)。
当我通过phpmyadmin查看数据库时,它会正确显示所有字符并显示正确的编码(UTF-8),所以我不知道问题出在哪里。
我该如何解决这个问题?
更新:
所以我做了这个,它有效:
protected function _initDb()
{
$this->configuration = new Zend_Config_Ini(APPLICATION_PATH
. /configs/application.ini ,
APPLICATION_ENVIRONMENT);
$this->dbAdapter = Zend_Db::factory($this->configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
$stmt = new Zend_Db_Statement_Pdo($this->dbAdapter,
"SET NAMES utf8 ");
$stmt->execute();
}
是否有更好的办法?
更新2:
我尝试过这个。
protected function _initDb()
{
$this->configuration = new Zend_Config_Ini(APPLICATION_PATH
. /configs/application.ini ,
APPLICATION_ENVIRONMENT);
$this->dbAdapter = Zend_Db::factory($this->configuration->database);
$this->dbAdapter = Zend_Db::factory($this->configuration->database->adapter,
$this->configuration->database->params->toArray()
+ array( driver_options => array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 ")));
Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
}
我犯了一个错误:
Fatal error: Undefined class constant MYSQL_ATTR_INIT_COMMAND in C:wampwwwakalarkaapplicationBootstrap.php on line 46