1

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: https://i.stack.imgur.com/sGgK8.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.

user1489920
  • 25
  • 1
  • 4

1 Answers1

2

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.

adamors
  • 2,672
  • 5
  • 32
  • 45
  • Already did set charset to utf8 and even put in `'initSQLs'=>array('set names utf8')` for good measure (even though I already set my mysql names, including connection, to utf8 by default). Also read through the wiki and did everything. Or should I even go through and change the yii core files to UTF-8 without a BOM? I assumed they would have been that way since download. – user1489920 Aug 04 '12 at 16:14
  • No, that isn't necessary. The other thing I can think of, is the encoding of the page. Check your headers. If that doesn't solve it, your data may in fact be in some other encoding. – adamors Aug 04 '12 at 16:27
  • Headers are being sent for sure since the kana/kanji displays both when I change the language to 'ja' and in the mysqli fetched table. The data is probably UTF-8 too, since the data that displays correctly from mysqli is the same as what yii is fetching. Oh, and I checked the CREATE statement for the table...definitely utf8. Only thing I can think is that I must be missing something with `CActiveDataProvider` in the controller or with the model in general. – user1489920 Aug 04 '12 at 16:55
  • The connection encoding can only be set in the database config file. Is mysqli using an utf-8 connection also? Maybe it isn't, and the data is really in latin1. Can you check the table with something like `phpmyadmin`? If you can, do you see the text properly, or is it messed up there as well? – adamors Aug 04 '12 at 17:00
  • 1
    Ah! Thank you so much. It was a bad assumption on my part that my database connection was utf8. Not quite sure on what I exactly had done with storing the data originally, but once I removed the charset and initSQL portion of my db connection in yii it worked. I guess I must have stored the kanji in hex or something...well, anyway, thanks again! Helped a lot. – user1489920 Aug 04 '12 at 17:20