gpt4 book ai didi

php - Zend 框架 : How do I plug my procedural solution for cyrillic (russian) chars into my ZF project?

转载 作者:行者123 更新时间:2023-11-29 00:51:26 25 4
gpt4 key购买 nike

我在我的第一个 ZF 项目中,遇到了带有俄文字符的砖墙。(参见 my original post on StackOverflow here)我切换到简单的旧过程 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);

[/UPDATE]

最佳答案

我对法语重音字符也有同样的问题。我的数据库是 utf8_general_ci,我的连接也是 UTF-8,就像你一样。

为了让它工作,我必须在我的 Bootstrap 中的 _initDb 中运行以下代码:

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);
}

关于php - Zend 框架 : How do I plug my procedural solution for cyrillic (russian) chars into my ZF project?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8419955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com