2010-09-10 193 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");