1

I'm trying to make sure the data received from my database is in utf-8, using Zend Frameword 2. To do so, I encoded my php files in utf-8 and I set the meta charset to utf-8 (to make sure what shows on the page is in utf-8).

I also tried to set my adapter's charset to utf-8 but it doesn't seem to have any effect on the data. I tried many ways:

$adapter = new Adapter(array(
        'driver'   => 'pdo_mysql',
        'database' => $database,
        'username' => $username,
        'password' => $password,
        'charset' => 'utf8'
));

$adapter = new Adapter(array(
            'driver'   => 'pdo_mysql',
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'charset' => 'utf-8'
    ));

$adapter = new Adapter(array(
            'driver'   => 'pdo_mysql',
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'driverOptions' => array(
                    1002=>'SET NAMES utf8'
            )
    ));

I fetch the data using a TableGateway object. Should I specify in that object that it is in utf-8? (but I didn't see a property to do so in that object)

Here is how I fetch the data:

$results = $this->tableGateway->selectWith($select);
if(count($results) > 0){
    //this loop transforms each object into an audio
    foreach($results as $row){
        $data['object'][$row->id] = $row;
    }
}

I pass the data in my Controller using:

$view->setVariable('arAudios', $arAudios);

I then print the data in the view using a print_r:

<?php echo '<pre>'; print_r($arAudios); echo '</pre>';?>

The data written directly on the page as well as other variables passed from the controller to the page are encoded correctly, but the data retrieved from the database shows ? where special characters should be. They show correctly if I use utf8_encode(), so I assume the data was not encoded when fetched from the database.

Anyone has an idea?

4

1 に答える 1

1

グローバル設定を次のように行うだけです

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=singclub;host=localhost;',
        'username'       => 'root',
        'password'       => '',
        'driver_options' => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"
        ),
    ),
);
于 2013-09-29T21:45:19.263 に答える