0

The character é appears as a question mark in Chrome. I'm using PHP to get the content from MySQL to output to HTML. I tried using mb_detect_encoding() to determine the contents' encoding, some are ASCII and some are UTF-8. Why do they have different encodings? Is MySQL making them different or is PHP doing this?

The content containing the é is encoded as UTF-8. But if I use utf8_encode() on the content, the character displays correctly. If it's already UTF-8, why does using utf8_encode() make it display correctly?

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284

2 Answers2

0

You have to define the connection to your db as utf8:

// Setup your connection
$connection = mysql_connect('localhost', 'user', 'pw');
mysql_select_db('yourdb', $connection);
mysql_query("SET NAMES 'utf8'", $connection);

// now you get utf-8 encoded stuff
$query = sprintf('SELECT name FROM place where id = 1');
$result = mysql_query($query, $connection);
$result = mysql_fetch_assoc($result);
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37
0

You can escape string using the htmlentities() PHP function so that the characters are changed into their respective HTML entities, such as è changes to è, regardless of if you are specifying an encoding type.