0

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.

abc
  • 133
  • 7

1 Answers1

4

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:

  • Make sure every source file is saved as UTF-8
  • Ensure <meta charset="UTF-8" /> is present on all pages
  • Make sure every database table is set to use UTF-8
  • Run SET NAMES utf8 after connecting to the database
  • Call mb_internal_encoding("UTF-8") at the beginning of the script and use mb_ string functions where possible
Community
  • 1
  • 1
Mate Solymosi
  • 5,699
  • 23
  • 30
  • Thank you so much, it really solved my problem. but I don't understand why it is necessary to run this function. even that mysql db is already set to be utf8? but thanks a lot, it working now :) – abc Nov 04 '13 at 13:59
  • It is not enough to set the collation of the table to UTF-8, you also need to specify the encoding that should be used by PHP and the MySQL server *during their communication*. Added this to my answer. – Mate Solymosi Nov 04 '13 at 14:06