PHP PDO: charset, set names? PHP PDO: charset, set names? php php

PHP PDO: charset, set names?


You'll have it in your connection string like:

"mysql:host=$host;dbname=$db;charset=utf8"

HOWEVER, prior to PHP 5.3.6, the charset option was ignored. If you're running an older version of PHP, you must do it like this:

$dbh = new PDO("mysql:$connstr",  $user, $password);$dbh->exec("set names utf8");


Prior to PHP 5.3.6, the charset option was ignored. If you're running an older version of PHP, you must do it like this:

<?php    $dbh = new PDO("mysql:$connstr",  $user, $password);    $dbh -> exec("set names utf8");?>


This is probably the most elegant way to do it.
Right in the PDO constructor call, but avoiding the buggy charset option (as mentioned above):

$connect = new PDO(  "mysql:host=$host;dbname=$db",   $user,   $pass,   array(    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"  ));

Works great for me.