I recently switched to PDO for many projects (used mysql/i drivers for too much time to be told) and encountered a strange behavior with the PDO Mysql driver and the charset.
I found out on StackOverflow that THE correct way to intialize a utf8 connection using pdo and mysql is to add Charset=utf8
at the end of the DSN. Yet this parameter was silently ignored in php version prior to 5.3.6.
Since I'm developing a cms, and I won't know which environment it will run on; I'm searching for a solution to make it compatible.
The first answer I found is use $PDO->exec("SET NAMES utf8");
Wouldn't that be the same of issuing a query, so, it would mess up thing with the escape functions.
The second answer is to set PDO::MYSQL_ATTR_INIT_COMMAND
to SET NAMES utf8
, so wouldn't that be the same as before ? (and so break escape functions ?)
Actually I'm getting a bit confused about this, and so I decided to ask for help. I've also thought of two solutions:
1) Build an abstraction layer and in case of PHP < 5.3.6 use mysqli driver instead
2) Utilize another charset and encode/decode each time the data are passed through the socket
Thanks