A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Repost: UTF-8, PHP and MySQL

This is a repost from Akrabat.com

The Problem:Get a £ (that is the pound sign) character stored to MySQL, retrieved and then displayed without any weird characters in front of it using UTF8.

//for the browser//header("Content-type: text/html; charset=utf-8");//OR////then for MySQLmysql_set_charset('utf8');//ormysqli_set_charset('utf8');//or execute the SQL immediately after connection://SET NAMES UTF8;//for PDO$handle = new PDO("mysql:host=localhost;dbname=dbname",    'username', 'password',    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));//and finally for Zend_Db$params = array(    'host' => 'localhost',    'username' => 'username',    'password' => 'password',    'dbname' => 'dbname',    'driver_options' => array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;'););$db = Zend_Db::factory('PDO_MYSQL', $params);

A million thanks to Rob for this post.

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>