Tuesday, 5 July 2016

utf 8 - Yii And UTF8 Display, UTF8 works with mysqli but not yii backend



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:




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.


Answer




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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...