Vincent Vincent - 6 months ago 18
MySQL Question

é instead of "é", definitely don't know why

I have a website displaying é characters instead of "é".

I found this thread How to support UTF-8 completely in a web application which is great but did not help me. I can't see why.

Data is been pulled from a Mysql database. SHOW VARIABLES gives :

character_set_client : utf8
character_set_connection : utf8
character_set_database : utf8
character_set_filesystem : binary
character_set_results : utf8
character_set_server : utf8
character_set_system : utf8
character_sets_dir : /usr/share/mysql/charsets/
collation_connection : utf8_unicode_ci
collation_database : utf8_general_ci
collation_server : utf8_general_ci

My php Header + PDO::connexion code is the following, setting utf-8 as charset :

<?php header("Content-type: text/html; charset=utf-8"); ?>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
$db = new PDO( 'mysql:host=host.mysql.db;dbname=dbname;charset=utf8', 'user', '');
$db->exec('SET NAMES utf8');

Then my PDO:query code is the following, I don't specify charset but I don't see why / how to do it :

$query="SELECT * FROM db where rank = 1 limit 30";
$req = $db->query($query) or die(print_r($db->errorInfo(), true));
$data1 = array();
$data1 = $req->fetchAll();

Finally I print data, giving é characters.

<?php for($i = 0; $i < 5; ++$i):
$title = utf8_encode($data1[$i]['title']);
echo mb_check_encoding($title, "UTF-8"); // prints 1 (True)
echo $title; // prints é

Note : If I use "utf8_decode()" on "data" instead of "utf8_encode()", I get � characters.

I am using Notepad++ and my files are encoded "UTF8 without BOM".

HINT : It works on localhost database. I put it online (OVH) and now I have these é characters.

On this local database, SHOW VARIABLES gives :

character_set_client : utf8mb4
character_set_connection : utf8mb4
character_set_database : latin1
character_set_filesystem : binary
character_set_results : utf8mb4
character_set_server : latin1
character_set_system : utf8
character_sets_dir : c:\wamp64\bin\mysql\mysql5.7.11\share\charsets\
check_proxy_users :OFF
collation_connection : utf8mb4_unicode_ci
collation_database : latin1_swedish_ci
collation_server : latin1_swedish_ci


You data should already be encoded in UTF-8 so you shouldn't have the utf8_encode function there.

The function utf8_encode encodes an ISO-8859-1 string to UTF-8, and this is not what you need.

Just remove this function and it should work.