我在我的第一个ZF项目中,遇到了一个带有俄罗斯字符的砖墙。(参见我在StackOverflow上的原始帖子)我切换到简单的旧程序PHP来解决它,现在我可以使用一点帮助来将我的解决方案实现到我的ZF项目中。
// in application.ini
resources.db.adapter = pdo_mysql
resources.db.params.host = localhost
resources.db.params.username = user
resources.db.params.password = pass
resources.db.params.dbname = db
resources.db.params.charset = UTF8
resources.db.params.driver_options.1002 = "SET NAMES utf8"
// in bootstrap
public function initDb(){ // For Testing ---->
/* Configures PDO to execute the 'SET NAMES UTF8;' SQL query just before any
other query. If no query is executed on your page, this will not be executed. */
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
$params = array(
'host' => 'localhost', // TODO: get from application.ini
'username' => 'user', // TODO: get from application.ini
'password' => 'pass', // TODO: get from application.ini
'dbname' => 'db', // TODO: get from application.ini
'driver_options' => $pdoParams
);
try {
$db = Zend_Db::factory('PDO_MYSQL', $params);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
} catch (Exception $e) {
exit($e->getMessage());
}
Zend_Registry::set('dbAdapter', $db);
}
// my data table gateway
class Model_DbTable_Books extends Zend_Db_Table_Abstract {
protected $_name = 'book';
protected $_primary = 'id';
}
// my controller action
public function listAction(){
$DbTableBooks = new Model_DbTable_Books();
$this->view->allBooks = $DbTableBooks->fetchAll();
}
// my view script
echo'<pre>'; print_r($this->allBooks); echo'</pre>'; // For Testing ---->
(更新)下面是我的表的SQL:
CREATE TABLE IF NOT EXISTS `book` (
`id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`parentID` int(3) unsigned DEFAULT NULL,
`russian` varchar(24) NOT NULL,
`english` varchar(24) NOT NULL,
PRIMARY KEY (`id`),
KEY `parentId` (`parentID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3;
INSERT INTO `book` (`id`, `parentID`, `russian`, `english`) VALUES
(1, 0, 'Мэри имела', 'Mary had'),
(2, 0, 'Небольшой ягненок', 'a little lamb');
下面是有效的过程代码:
/* SANDBOX */
if(!$link = mysql_connect('localhost','user','pass')) die('Could not connect: ' . mysql_error());
mysql_set_charset('UTF8',$link);
if (!$db = mysql_select_db('db', $link)) die ("Can't use $_DB : " . mysql_error());
$result = mysql_query('SELECT * FROM book');
while($row = mysql_fetch_assoc($result)) {
echo'<pre>';print_r($row);echo'</pre>'; // For Testing ---->
}
mysql_close($link);
(/更新)我对法语重音字符也有同样的问题。我的数据库在utf8_general_ci
,我的连接在UTF-8
,和你一样。
为了使它工作,我必须在我的_initDb
中运行de以下代码:
protected function _initDbAdapter()
{
$this->bootstrap('db');
$db = $this->getPluginResource('db');
// force utf-8 charset
$stmt = new Zend_Db_Statement_Pdo(
$db->getDbAdapter(),
"SET NAMES 'utf8'"
);
$stmt->execute();
$dbAdapter = $db->getDbAdapter();
Zend_Registry::set('db', $dbAdapter);
}
可能听起来很傻,但尝试在"中输入UTF8,例如:
resources.db.params.charset = "UTF8"
就是:
resources.db.adapter = "Pdo_Mysql"
resources.db.params.charset = "UTF8"
resources.db.params.username = "woot"
resources.db.params.password = "forever_alone"
resources.db.params.dbname = "wooot_10000"
resources.db.params.host = "localhost"