之前已經提到過這個問題,但另一位發佈者的擔心更多地與原始文檔的編碼有關。在網頁上顯示中文文本
我有一個包含一些中文文本的MySQL數據庫。我在這些領域使用的排序規則是utf8_unicode_ci
。使用phpMyAdmin並瀏覽數據庫,中文文本應該顯示。當我嘗試在網頁上顯示這些文字時,我會得到?????在它的地方。這裏是我的代碼:
<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...
有什麼我失蹤了嗎?我也嘗試將PHP header()
調用移動到<html>
標籤之上,但這也不起作用。
編輯:這裏是數據庫代碼:
$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:所以我與閱讀在中國存儲字符作爲UTF-8使用線mysql_set_charset('utf8', $con);
解決的問題,但我我發現當我想添加到這個數據庫時,中文文本顯示爲?????再次。舉例來說,如果我是中國節能的字符串作爲變量
$chinese = "炒豬肉與蔬菜";
echo "$chinese<br>";
這說明?????再次。任何線索?
謝謝。
顯示數據庫的代碼,這可能是問題的所在 –
'的mysql_query(「SET NAMES UTF8」);' – Giberno
請仔細閱讀[處理Unicode前端在Web應用程序中](http://kunststube.net/frontback/)。 – deceze