Skip to content

UTF-8 Special Character Problems With MySQL

Short version:

Always use a query

SET NAMES <charset>

for instance, I used

SET NAMES utf8

before accessing the database. Preferably add it right after connecting to the database.

Long version:

For a several months I've been fighting with character sets. For some reason querying a UTF-8 MySQL database gave me different results than showing the database contents in phpMyAdmin or CocoaMySQL. Special characters, in this case, scandinavian ones, showed up either

  • all scrambled in database queries, yet quite perfectly in admin tools, or
  • perfectly in queries, but messed up in admin tools.

Because ASCII characters, that is, the majority of all used ones, show properly with pretty much any charset, I had been able to avoid facing this subject, or at least not finding an easy solution. Until today.

I realized this was going to be a whole lot of extra work that I did not want to charge from my clients. So I decided to fix this issue no matter what. After searching about half an hour I bumped into a blog entry that showed the solution. Fairly easy, eh? More info in MySQL Documentation.