我的数据库中的表是使用正确的 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:\wamp\www\bakalarka\application\Bootstrap.php on line 46