UTF-8 with MySQL and PDO

If you are using PDO (PHP Data Objects) and you’re having problems with utf-8, try to add PDO::MYSQL_ATTR_INIT_COMMAND option inside PDO definition like I wrote below.

$db = new PDO(
    'mysql:host=example.com;dbname=dbName',
    "username",
    "password",
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

Leave a Reply

Your email address will not be published. Required fields are marked *