Yii和UTF8显示,UTF8与mysqli一起使用,但与yii后端不兼容 [英] Yii And UTF8 Display, UTF8 works with mysqli but not yii backend
问题描述
我一直在努力让Yii正确显示utf8,即使我的apache服务器和数据库都已设置为正确显示它.
I've been struggling to get Yii to correctly display utf8 even though both my apache server and my database are setup to correctly display it.
更具体地说,我有一组从mysql数据库中提取的汉字/假名数据.作为对此的简单测试,我设置了一个视图页面,以使用mysqli和yii的mvc架构从其中提取数据.当用mysqli拉时,它显示正确,而当用yii拉时,它不能正确显示.这是来自相同的确切页面,这也意味着它是来自相同的确切控制器. 我是否需要强制模型后端使用utf8编码?
More specifically, I have a set of kanji/kana data that I'm pulling from a mysql database. As a simple test on it, I set up a view page to pull data from it with mysqli and with yii's mvc architecture. When pulled with mysqli, it displays correctly while when pulled with yii it does not. And this is from the same exact page which also means it's from the same exact controller. Am I missing something with forcing the model backend to use utf8 encode?
图片: http://i.imgur.com/BLmrP.png
这是我在视图文件中使用yii的mvc东西的代码:
Here's the code I'm using in the view file for yii's mvc stuff:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'enablePagination' => false,
)); ?>
然后mysqli就是您所期望的.
And then the mysqli is about what you'd expect.
任何帮助将不胜感激.
Any help would be appreciated.
推荐答案
您必须指定用于数据库连接的字符集.这是在您的主配置文件(默认为protected/config/main.php
)中完成的,该文件类似于
You must specify the charset to use for a database connection. This is done in your main config file (by default this is protected/config/main.php
) which looks something like this
return array(
......
'components'=>array(
......
'db'=>array(
'connectionString'=>'sqlite:protected/data/source.db',
'charset'=>'utf8',
),
),
......
);
如果未显式设置charset
参数,则连接通常为latin1,这就是为什么看到乱码的原因.
If the charset
parameter is not explicitly set, the connection is usually latin1, that is why you are seeing gibberish.
此外,还有一个 wiki条目,关于正确设置Unicode .
这篇关于Yii和UTF8显示,UTF8与mysqli一起使用,但与yii后端不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!