UTF8 characters from database don't show up properly in the browser - MySQL & PHP CodeIgniter UTF8 characters from database don't show up properly in the browser - MySQL & PHP CodeIgniter codeigniter codeigniter

UTF8 characters from database don't show up properly in the browser - MySQL & PHP CodeIgniter


Make sure your my.cnf (likely to be in /etc/) has the following entries :

[mysqld]default-character-set=utf8default-collation=utf8_general_cicharacter-set-server=utf8collation-server=utf8_general_ciinit-connect='SET NAMES utf8'[client]default-character-set=utf8

You'll need to restart the mysql service once you make your changes.

Adding my comments in here to make this a little clearer.

Make sure the following HTTP header is being set so the browser knows what charset to expect.

Content-type: text/html; charset=UTF-8

Also try adding this tag into the top of your html <head> tag

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />


To make the browser show up correctly.you should check three points:

  1. encoding of your script file.
  2. encoding of connection.
  3. encoding of database or table schema.

if all of these are compatible, you'll get the page you want.


The original data has been encoded as UTF-8, the result interpreted in Windows-1252 and then UTF-8 encoded again. This is really bad; it isn't about a simple encoding mismatch that a header would fix. Your data is actually broken.

If the data is ok in the database (check with SELECT hex(column) FROM myTable) to see if it was double encoded already in the database), then there must be your code that is converting it to UTF-8 on output.

Search your project for uses of function utf8_encode, convert_to_utf8, or just iconv or mb_convert_encoding. Running

$ grep -rn "\(utf8_\(en\|de\)code\|convert_to_utf8\|iconv\|mb_convert_encoding\)" .

On your application's /application folder should be enough to find something.

Also see config values for these:

<?phpvar_dump(    ini_get( "mbstring.http_output" ),    ini_get( "mbstring.encoding_translation" ));