PDO Connection

2017-09-25


$pdo = new PDO(
    'mysql:host=hostname;dbname=database;charset=utf8',
    'username',
    'password'
);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);


Dealt with the weirdest thing today... PDO was storing UTF8 strings oddly in MySQL, but was pulling them and fixing them on the fly (!?) -- Odd... All the things (headers, php.ini, MySQL collation, etc.) were set to UTF8, except for the ;charset=utf8 appended to the PDO connection DSN (which fixed the problem). @see this and this.