UTF-8: showing correctly in database, however not in HTML despite utf-8 charset
Solution 1
- Make sure you send the
SET NAMES utf8
SET NAMES utf8mb4
command to MySQL after connecting, before running any MySQL queries. - Make sure your page is actually rendered as utf-8 (if there's an HTTP header
Content-Type: text/html;charset=iso-8859-1
, browsers disagree about which should win). - Read this article: Handling Unicode Front To Back In A Web App (but remember to replace
utf8
withutf8mb4
where MySQL is concerned).
If phpMyAdmin displays your entered data as correct Unicode text, then my bet is that you are not doing SET NAMES utf8
after connecting.
Solution 2
Try use such code after connecting to DataBase, but befor you recieve data
$db->query('set character_set_client=utf8');
$db->query('set character_set_connection=utf8');
$db->query('set character_set_results=utf8');
$db->query('set character_set_server=utf8');
Chris
Updated on June 11, 2022Comments
-
Chris almost 2 years
I use MySQL 5.1 and loaded from a UTF-8 decoded txt-file about 2.7 mil lines into a table which itself is declared as
utf8_unicode_ci
and as well all char-fields are declared asutf8_unicode_ci
, usingLOAD DATA INFILE
...In the database itself the characters all seem to be correct, everything looks nice. However, when I print them using php, the characters show up as ???, although I use utf-8 declaration in the HTML head:
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ...
In another table (using utf-8), where I inserted text from a submitted form, the characters appear strangely in the database, but are shown correctly again, when I print them using
SELECT...
.So, I was wondering: what is wrong? Are
UTF-8
chars shown correctly in the database or strangely but when youSELECT
them again they are OK? Or where is the problem (when loading the file into the db, in the HTML or somewhere in between)??Thank you very much for any hint or suggestion! :)
-
Krista K almost 6 years@PraveenYadav just after
$db = new mysqli($host, $username, $password, $dbname);
-
Shivanshu over 5 yearsYou Sir just saved my day!! :)