This has been asked before, but the other poster’s concern had more to do with the original encoding of the document.
I have a MySQL database containing some Chinese text. The Collation I am using for those fields is utf8_unicode_ci. Using phpMyAdmin and browsing the database, the Chinese text appears as it should. When I try to display this text on a webpage however, I get ????? in it’s place. Here is my code:
<html>
<?php
header('Content-Type: text/html; charset=UTF-8');
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
// Code opening database and retrieving fields and such...
Is there something I am missing? I’ve also tried moving the PHP header() call to above the <html> tag but that doesn’t work either.
EDIT: Here is the database code:
$con = mysql_connect("localhost", "name", "pass") or die('Sorry, could not connect to database server');
mysql_select_db("database_name", $con) or die('Sorry, could not connect to database');
$query = "SELECT id,field1,field2 FROM table1 ORDER BY id ASC";
$result = mysql_query($query) or die('Sorry, could not get recipes at this time ');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$var1= $row['field1'];
$var2= $row['field2'];
echo"ID:$id The contents are $var1 and $var2.<br><br>\n";
}
UPDATE: So I’ve addressed the issue with reading in the stored Chinese characters as UTF-8 using the line mysql_set_charset('utf8', $con);, but I am finding that when I want to add to this database, the Chinese text shows up as ????? again. For instance, if I am saving a string of Chinese characters as a variable
$chinese = "炒猪肉与蔬菜";
echo "$chinese<br>";
This shows ????? again. Any clues?
Thanks.
This is a great resource: Handling Unicode Front to Back in a Web App
The fix for the initial question was to add the line
mysql_set_charset('utf8', $con);after the linemysql_select_db();.