Yii And UTF8 Display, UTF8 works with mysqli but n

2019-07-25 15:54发布

问题:

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.

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?

Image: http://i.imgur.com/BLmrP.png

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,
)); ?>

And then the mysqli is about what you'd expect.

Any help would be appreciated.

回答1:

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',
        ),
    ),
    ......
);

If the charset parameter is not explicitly set, the connection is usually latin1, that is why you are seeing gibberish.

Also, there's a wiki entry about setting up Unicode properly.



标签: utf-8 yii