RomanLaw: Lessons learned on character encoding and mySQL
Posted by mholmes on 11 Sep 2008 in Activity log
I have XHTML pages in UTF-8, with forms in UTF-8, submitting to a database whose tables/fields are all in UTF-8, and I'm retrieving data from it and sending it back from PHP to the AJAX machinery with a header that specifies UTF-8. AND YET I was getting garbage (question marks) for non-ASCII characters. Why?
mySQL needs to be told explicitly to output data as UTF-8. You do it like this, in PHP, after making the connection:
mysql_query( "SET NAMES 'utf8' " );
45 minutes wasted on this, so I'm blogging it for the next person who's tearing their hair out over it.