MySQL + UTF-8 + PHP 5.3.10 + JSON = Trouble

Yesterday, I spent longer than I care to admit debugging an update to an old PHP script. It fetched a string from a MySQL database and (here’s the new part) passed the string to json_encode(). That call failed when the data included the multibyte characters ¼ or ½ or ¾. All of my attempts to remove the multibyte characters from the strings (replacing them with “1/4″ or “1/2″ or “3/4″) failed.

The problem turned out to be PHP 5.3.10′s default for the character set of the MySQL connection and the fix was easy. Explicitly set the character set to UTF-8:

Once that was set, json_encode() properly recognized the string as UTF-8 and it ran without error.

This problem will not occur with newer versions of PHP. They have the default character set as UTF-8.

Posted in Software