I
I
iamsaint2014-12-01 11:40:29
MySQL
iamsaint, 2014-12-01 11:40:29

How to set the encoding for the second database in Yii?

Good afternoon.
I'm trying to set up a connection to the second database in the yii config:

'db2'=>[
            'class' => 'CDbConnection',
            'connectionString' => 'mysql:host=localhost;dbname=table2',
            'emulatePrepare' => true,
            'username' => 'user',
            'password' => 'pass',
            'charset' => 'utf8',
        ],

MySQL config
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
Base encoding:
utf8 -- UTF-8 Unicode
utf8_general_ci
Table encoding
utf8_general_ci
But when fetching
Yii::app()->db2->createCommad()->select('title')->from('news')
Russian text is returned as question marks.
At the same time, if in the config you change 'db2'=>[to 'db'=>[, then
Yii::app()->db->createCommad()->select('title')->from('news')
outputs the text in the correct encoding.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Zelenin, 2014-12-01
@zelenin

it is strange that it returns, because the command is written incorrectly.

D
Dmitry Vapelnik, 2014-12-11
@dvapelnik

do you display the same selections on the same page in the browser? most likely you have a problem with the fact that the charset for the page is not the one in the second database. therefore, everything is fine with the first database (utf-8 both in the database and in the browser), but with the second problem (utf-8 in the browser, but the text that you get from the database is in custom encoding)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question