4

I have just encountered something rather strange, I use the Zend Framework 1.10 with the Zend_Db_Table module to read some data from a databse. The database itself, the table and the fields in question all have their collation set to "utf8_general_ci" and all special chars appear correctly formatted in the DB when checked with phpMyAdmin. Also, saving with Zend_Db_Table works just fine, yet when I read the data and just echo it to my browser it is returned as ISO-8859-1, not as UTF8. I noticed the same thing when trying to use json_encode (which only works with UTF8 strings as input) on a value returned from the DB.

How can I set that Zend_Db_Table/Zend_Db_Row should always work with UTF8 and return me an UTF8 value? I have not set anything regarding encoding in my app yet.

Thanks a lot for your help!

Robin
  • 8,197
  • 11
  • 45
  • 74

4 Answers4

13

Just note. In my case this one helped:

    $this->db = new Zend_Db_Adapter_Pdo_Mysql(array(
        'host'     => $config['db_hostname'],
        'username' => $config['db_username'],
        'password' => $config['db_password'],
        'dbname'   => $config['db_database'],
        'charset'  => 'utf8'
    ));
Dionysius
  • 488
  • 5
  • 10
  • This didn't for for me, however as @robertbasic commented below "resources.db.params.charset = utf8" in the app ini fixed the problem. – ktamlyn Feb 21 '14 at 19:11
  • Making this the accepted answer since the upvotes clearly favor this solution (along with @robertbasic's comment) – Robin May 07 '14 at 08:53
5
resources.db.params.charset = utf8

like robertbasic said.

armandfp
  • 1,059
  • 1
  • 9
  • 18
1

Ok just found the solution, try to do this:

$db = Zend_Db::factory($config->database); // Setting up the DB
$db->query("SET NAMES 'utf8';");           // That's the magic line I was missing

Hope this helps somebody else at some point :)

Robin
  • 8,197
  • 11
  • 45
  • 74
0

Also you can just put "charset" key with desired value into config and DB-driver will execute an appropriate query (depending on DBMS used). It seems to be that currently (version 1.10.5) almost all drivers support that.

Sergei Morozov
  • 602
  • 1
  • 7
  • 17