How to use UTF-8 with MySQL and PHP Data Objects (PDO)
If you are using PHP Data Objects (PDO) and are having trouble getting UTF-8 out of your MySQL server, even though everything is set to UTF-8 explicitly, you might want to try this:
$pdo = new PDO(
'mysql:host=mysql.example.com;dbname=example_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
The last parameter of the PDO constructor call is the driver specific options, given as an array with key => value pairs. The MySQL driver have a “PDO::MYSQL_ATTR_INIT_COMMAND” option where you can specify a command that is executed every time you connect to the database. The MySQL specific query “SET NAMES utf8” is simply telling MySQL to use UTF-8 as the character set for our connection.
Read more:
Kommentera inlägget

programmering