phphtmlmysqlencodingmb-convert-encoding

why do I need mb_convert_encoding to show the correct characters


my website have charset=UTF-8, and mysql DATABASE is also set to be UTF-8 encoding.

my problem is that when I retrieve text from mysql to the website, i need to use php function mb_convert_encoding(@db_field,'utf-8','iso-8859-1') to show the text properly. it looks like that system think mysql DB is in iso-8859-1.


Solution

  • Run this right after you connect to the database:

    mysqli_query("SET NAMES utf8"); /* or mysql_query */
    

    It is not enough to set the collation of the table to UTF-8, you also need to specify (with this query) the encoding that should be used during the communication between PHP and the MySQL server.
    More details here: SET NAMES utf8 in MySQL?

    Ensuring UTF-8 compatibility everywhere is tricky; my UTF-8 checklist is as follows: