Share/Save/Bookmark Subscribe

Monday, May 24, 2010

Displaying UTF8 from MySQL with PHP

I had some issues displaying localized text in a PHP based internal tool recently, it turns out the solution was really simple-the MySql connection just needs to know that the connection must be UTF8. Probably no-brainer stuff for all you PHP-Ninjas.. news to me :)

To do this with a PDO connection, just add an init command when you create the PDO command:

1 2 3 
$this->pdo = new PDO("mysql:host=".$this->host."; dbname=".$this->databaseName,
$this->user, $this->password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

Thanks to Rob Allen for the tip, variation for other connection methods can be found on his blog.

Posted via email from Matt's thoughts

blog comments powered by Disqus
 

Copyright 2007 All Right Reserved. shine-on design by Nurudin Jauhari. and Published on Free Templates

Afrigator