2010-09-10 170 views
8

我有內容閱讀UTF-8從MySQL表的內容

結構的mysql表是在這裏:

alt text

現在我有它的一個紀錄:

alt text

我要閱讀並打印此表格的內容到html 這是我的代碼:

<?php 

    include("config.php"); 
    $global_dbh = mysql_connect($hostname, $username, $password) 
    or die("Could not connect to database"); 
    mysql_select_db($db) 
    or die("Could not select database"); 
    function display_db_query($query_string, $connection, $header_bool, $table_params) { 
     // perform the database query 
     $result_id = mysql_query($query_string, $connection) 
     or die("display_db_query:" . mysql_error()); 
     // find out the number of columns in result 
     $column_count = mysql_num_fields($result_id) 
     or die("display_db_query:" . mysql_error()); 
     // Here the table attributes from the $table_params variable are added 
     print("<TABLE $table_params >\n"); 
     // optionally print a bold header at top of table 
     if($header_bool) { 
      print("<TR>"); 
      for($column_num = 0; $column_num < $column_count; $column_num++) { 
       $field_name = mysql_field_name($result_id, $column_num); 
       print("<TH>$field_name</TH>"); 
      } 
      print("</TR>\n"); 
     } 
     // print the body of the table 
     while($row = mysql_fetch_row($result_id)) { 
      print("<TR ALIGN=LEFT VALIGN=TOP>"); 
      for($column_num = 0; $column_num < $column_count; $column_num++) { 
       print("<TD>$row[$column_num]</TD>\n"); 
      } 
      print("</TR>\n"); 
     } 
     print("</TABLE>\n"); 
    } 

    function display_db_table($tablename, $connection, $header_bool, $table_params) { 
     $query_string = "SELECT * FROM $tablename"; 
     display_db_query($query_string, $connection, 
     $header_bool, $table_params); 
    } 
    ?> 
    <HTML><HEAD><TITLE>Displaying a MySQL table</TITLE></HEAD> 
    <BODY> 
    <TABLE><TR><TD> 
    <?php 
    //In this example the table name to be displayed is static, but it could be taken from a form 
    $table = "submits"; 

    display_db_table($table, $global_dbh, 
    TRUE, "border='2'"); 
    ?> 
    </TD></TR></TABLE></BODY></HTML> 

,但我得到????????作爲結果: 這裏是輸出: alt text

我的錯誤在哪裏?

回答

18

四項好措施,始終得到正確UTF-8編碼的文本:

1)運行任何其他查詢之前此查詢:

mysql_query("set names 'utf8'"); 

2)添加到您的HTML頭:

01:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 

3)在你的PHP代碼頂部添加此

4)使用Notepad++或任何其他良好的文本編輯器/ IDE,使用UTF-8 without BOM編碼保存文件。

+0

謝謝你明白了:D – Alireza 2010-09-10 05:30:58

+0

不客氣。 – shamittomar 2010-09-10 05:32:03

+0

不僅可以設定名稱,還可以整理。 – 2010-09-10 07:39:21

5

您沒有將您的HTML頁面定義爲UTF-8。請參閱this question關於如何做到這一點。

可能也需要將您的數據庫連接顯式設置爲UTF8。做一個

mysql_query("SET NAMES utf8;"); 

^ 說得對您的數據庫連接下的腳本或包括,確保你有你做任何必要的查詢前放置。另外,對於搭配,請花時間確保您的 將其設置爲適合您的語法類型,並且general_ci在使用時似乎對我有用。作爲一個壓軸,清理你的頭緩存後,將瀏覽器設置爲正確的編碼工具欄 - >查看 - >編碼

建立連接後,將連接設置爲妥善處理問題。如果第一步已經工作,不要這樣做。

+0

感謝的人!你很棒! – Alireza 2010-09-10 05:54:05

0

試試這個:

mysql_set_charset('utf8', $yourConnection); 
0

舊方式已過時。如果你正在使用PHP> 5.0.5使用的mysqli的new syntax現在是:

$connection->set_charset("utf8") 

哪裏$connection是您連接到數據庫的引用。

2

設置字符集爲UTF8如下:

$conn = new mysqli($servername, $username, $password, $dbname); 
$conn->set_charset("utf8");