How to specify Charset with PDO/MySQL

4 messages Options
Embed this post
Permalink
Christian Wittwer2

How to specify Charset with PDO/MySQL

Reply Threaded More More options
Print post
Permalink
Dear List,
First of all, thanks for your great work on the database components!

I'm using the PDO_mysql adapter to access a mysql-database (5.0.22). All
tables in the db are UTF-8 encoded.
When I access with phpmyadmin, selecting charset utf-8 at the login, all
works great.
But with my own app using ZF and PDO_mysql, umlauts are not displayed
correct.

So, how can I specify, that PDO_mysql will use charset utf-8?

Regards,
Chris
Martin Martinov-2

Re: How to specify Charset with PDO/MySQL

Reply Threaded More More options
Print post
Permalink
Hello,
you can use one of those queries:

set names utf8;
set character set utf8;

I was going to describe what they are doing, but it's already here:
http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html

On 20/11/06, Christian Wittwer <[hidden email]> wrote:

> Dear List,
> First of all, thanks for your great work on the database components!
>
> I'm using the PDO_mysql adapter to access a mysql-database (5.0.22). All
> tables in the db are UTF-8 encoded.
> When I access with phpmyadmin, selecting charset utf-8 at the login, all
> works great.
> But with my own app using ZF and PDO_mysql, umlauts are not displayed
> correct.
>
> So, how can I specify, that PDO_mysql will use charset utf-8?
>
> Regards,
> Chris
>

--
Warm regards,
Martin Martinov
http://mmartinov.com/
Christian Wittwer2

Re: How to specify Charset with PDO/MySQL

Reply Threaded More More options
Print post
Permalink
Martin Martinov schrieb:
> Hello,
> you can use one of those queries:
>
> set names utf8;
> set character set utf8;
>
> I was going to describe what they are doing, but it's already here:
> http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
>

Ok, "set names utf8" solved my problem, but I have to pass this option
everytime I query the database?
I cannot use "default-character-set", because other php-apps on this
server still use latin1.
Any ideas?
Gunar Scholz

Re: How to specify Charset with PDO/MySQL

Reply Threaded More More options
Print post
Permalink
Christian Wittwer schrieb:
> Ok, "set names utf8" solved my problem, but I have to pass this option
> everytime I query the database?
> I cannot use "default-character-set", because other php-apps on this
> server still use latin1.
> Any ideas?

You can extend Zend_Db_Adapter_Pdo_Mysql.

My solution looks like this:

class Fx_Db_Adapter_PdoMysql extends Zend_Db_Adapter_Pdo_Mysql {
   protected function _connect() {
     if ($this->_connection)
       return;

     parent::_connect();

     // please do not touch names if I don't tell you to do so
     $this->_connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);

     $this->query('SET NAMES utf8');
   }
}

If you don't want to name your class Zend_Db_Adapter_Pdo_...
or Zend_Db_Adapter_..., because it's not a class provided by Zend, you
can't use Zend_Db::factory(). Instead you have to instantiate your
adapter, i.e.: $db = new Fx_Db_Adapter_PdoMysql($config);


Gunar