简体   繁体   中英

MySQL UTF-8 error using Zend Framework 2

I have a zf2 project that records clients informations in a mysql database. My DB is configured with utf8 charset and utf8_unicode_ci .

My ZF2 global.php is this

return array(
    'db' => array(
        'username' => 'root',
        'password' => 'root',
        'driver'   => 'Pdo',
        'dsn'      => 'mysql:dbname=atendimento;host=localhost',
        'charset'  => 'utf-8',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories'   => array(
            'Zend\Db\Adapter\Adapter'
                      => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
);

In the browser, when i submit the inputs, it goes to a ZF2 error "Statement could not be executed" and next SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\\xE3' for column 'column' at row 1

Sorry if this question have already been answered, but i didnt find an answer, i hope you can help me.

return new \Zend\Db\Adapter\Adapter ( array (
                'driver' => 'pdo',
                'dsn' => "mysql:dbname=$config[Dbname];host=$config[host]",
                'username' => $config ['Dbuser'],
                'password' => $config ['Dbpassword'],
                'driver_options' => array(
                        \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
                ),
        ) );

This is correct config from global and work for me

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

my friend helped me

The solution was to set utf8_encode($data) before recording, and utf8_decode($data) in the view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM