SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP UTF-8 problem

    Hello guys,

    I'm currently building multilanguage website and I have a strange problem when I try to print messages in another languages. For example, when I get russian translations from MySQL database and trying to print them with PHP, browser shows just "???? ??????? ?????" instead of the message in russian.

    My MySQL tables are UTF-8 by default (character set is set to "UTF-8 Unicode" and collation is set to "utf8_general_ci").

    My HTML pages have also metatag to show UTF-8:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

    I've googled for it, and found one advice. I've added "mysql_query("SET NAMES utf8");" into my PHP script before other MySQL queries instead of "???? ??????? ?????" I get a lot of ugly characters in the browser now like "Добавить место для посещения".

    I think it's PHP/MySQL problem, not browser or HTML. I wonder how can I fix it?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try using UTF16, it has a wider range of character support.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    try using UTF16, it has a wider range of character support.
    Thanks for advice. But I don't think it will help, also everybody use UTF-8 for all languages including russian, chinese, japanese etc.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2007
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you're inserting and selecting data from the database use:

    SET NAMES 'utf8'

    Prior to any statements requiring the insertion or extraction of UTF-8 encoded data.

  5. #5
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Speeple View Post
    When you're inserting and selecting data from the database use:

    SET NAMES 'utf8'

    Prior to any statements requiring the insertion or extraction of UTF-8 encoded data.
    Yes, I wrote above about I've tried this. When I tried to insert this code, "?????..." changed to "Добавить...".

  6. #6
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By doing "SET NAMES utf8", it would work. I do it for Greek and works.

    Are your html pages charset=utf-8? tables' collation to utf8?

  7. #7
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by _Steve_ View Post
    By doing "SET NAMES utf8", it would work. I do it for Greek and works.

    Are your html pages charset=utf-8? tables' collation to utf8?
    Yes, please look my first post. Table are in Utf-8 and HTML is in UTF-8 too. But it doesn't work.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    are you sending the proper HTTP header?
    header('Content-Type: text/html; charset=utf-8');
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Addict
    Join Date
    Aug 2007
    Location
    GR
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I can only think of setting the encoding from the browser as a last resort.

  10. #10
    SitePoint Guru
    Join Date
    Mar 2004
    Posts
    639
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    are you sending the proper HTTP header?
    header('Content-Type: text/html; charset=utf-8');
    Awesome. Thanks. I've killed whole day on this. It works perfectly now.

    Thanks again.

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > try using UTF16, it has a wider range of character support.

    I wouldn't as you don't particularly know that it is UTF-8 that is the issue, so why complicate the matter? Besides, most of the time UTF-8 is more than adequate in any case.

    > It works perfectly now.

    I would use an .htaccess file to send the appropriate header instead but I suppose at the end of the day, it doesn't matter either way.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •