This section describes how to fetch Chinese text from MySQL database and send it to a Web page through Apache in Big5 encoding.
Of course, I need to repeat the same test with Big5 encoding. Here is the test PHP script:
<?php #MySQL-Web-Chinese-GBK.php
# Copyright (c) 2007 by Dr. Herong Yang, http://www.herongyang.com/
#
$con = mysql_connect("localhost", "Herong", "TopSecret");
$ok = mysql_select_db("HerongDB", $con);
$test_name = "Moving Chinese Text from Database to Web";
print('<html>');
print('<meta http-equiv="Content-Type"'.
' content="text/html; charset=big5"/>');
print('<body><pre>'."\n");
# Delete the record
$sql = "DELETE FROM Comment_Mixed WHERE Test_Name ='$test_name'";
mysql_query($sql, $con);
print("\nNumber of rows deleted: ".mysql_affected_rows()."\n");
# Build the SQL INSERT statement
$sql = <<<END_OF_MESSAGE
INSERT INTO Comment_Mixed (Test_name, String_ASCII,
String_Latin1, String_UTF8, String_GBK, String_Big5)
VALUES ('$test_name', 'Television',
X'54E96CE9766973696F6E',
X'E794B5E8A786E69CBA2FE99BBBE8A696E6A99F',
X'B5E7CAD3BBFA',
X'B971B5F8BEF7');
END_OF_MESSAGE;
mysql_query($sql, $con);
print("\nNumber of rows inserted: ".mysql_affected_rows()."\n");
# Set character_set_results
mysql_query("SET character_set_results=big5", $con);
# Fetch text data from database
$sql = "SELECT * FROM Comment_Mixed"
. " WHERE Test_Name = '$test_name'";
$res = mysql_query($sql, $con);
# Send text data output
if ($row = mysql_fetch_array($res)) {
print("\nTest Name = ".$row['Test_Name']."\n");
print(" String_ASCII: ".$row['String_ASCII']."\n");
print(" String_Latin1: ".$row['String_Latin1']."\n");
print(" String_UTF8: ".$row['String_UTF8']."\n");
print(" String_GBK: ".$row['String_GBK']."\n");
print(" String_Big5: ".$row['String_Big5']."\n");
}
mysql_free_result($res);
mysql_close($con);
print('</pre></body></html>');
?>
But this time, only Big5 encoded Chinese characters are displayed correctly. Two simplified
Chinese characters encoded in UTF-8 and GBK were displayed as "?" marks.
This is caused by the limits of the Big5 character set. It does not contain any simplified
Chinese characters. MySQL still did a good job of converting GBK and UTF-8 Chinese characters
to Big5 Chinese characters. But it did not perform any mapping between simplified characters
and traditional characters.
You can find tools on the Internet that maps simplified Chinese characters to traditional Chinese
characters, if you are interested.
Also notice that the French character from the String_Latin1 column also failed to show up
on the Web page. This is again due to the limitation of the Big5 character set.