Preserve Your Data’s International Flavour

funky

When working with MySQL data sources that contains international characters (i.e., accents, Asian characters), it’s important that form data is entered using UTF-8 encoding and stored in a UTF-8 compliant table. It’s easy to overlook that the data also needs to be pulled out of MySQL using UTF-8, since the default database engine encoding may be set to a local standard like ASCII, even if a particular database or table is set to UTF-8 (or other flavor of Unicode).

Preventing international characters stored in MySQL from going all funky on output is actually pretty simple:

In PHP, we would need to initiation the connection to MySQL in the usual way:

$myQuery=mysql_connect ("mysql.exampledomain.com:3306", "username", "password");

But then add the following line:

mysql_set_charset("UTF8", $myQuery);

Now, all that lovely data should return with all its international flavour intact.

There’s more information about this technique over at Stack Overflow.