UFT-8 on a website based on mySQL and PHP

I spent quite some time today trying to get the text on a website with Bulgarian characters to show properly. The text is coming out of a MYSQL 5.0 database, configured to use utf8_general_ci and the website is generated by PHP 5.2.5.
I knew I had to use
meta equiv=”Content-Type” content=”text/html; charset=utf-8″
to display the Bulgarian characters properly (like Велико Търново) and that worked fine for static text, but I got ???????? when using the text from the database.
I tried all sorts of things, and visited various websites with proposed solution, but finally I got it working by one simple setting, explained by this website. I needed to include
mysql_query(‘SET NAMES utf8’);
when connecting to the database. That solved the issue.

5 Comments

  1. I had a simular issue using a webservice call that returned a XML file with SQL data. The only way to solve this was to set the charset to ISO 8859-1.
    I hope this works you.

  2. Maike,
    Thanks for your comments. That would indeed have been a simpler solution (once you know it :-)) if you have to deal with Western European characters only. In my case, I needed Bulgarian characters (like добър прием) and they are not available in ISO 8859-1. So I was forced to stick with utf-8, but then you need to use the rather obscure “SET NAMES utf8” setting in mysql. See http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html for details.
    Kris

  3. Pingback: Kris’s Box » Blog Archive » JavaScript uses url-encoded UTF-8 strings to perform Ajax POSTs

  4. Usefull.

    I also needed this headers to display bulgarian characters correctly from my DB:

    header(‘Content-Type: text/html; charset=utf-8’);
    mb_internal_encoding(“UTF-8”);

    Bye!

  5. I usually do not leave comments, but I thought this was a great post! Kris’s Box » Blog Archive » UFT-8 on a website based on mySQL and PHP was a wonderful read. WoW:)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.