从mysql表中读取utf-8内容

Ali*_*eza 10 php mysql utf-8

我有一个内容的mysql表

结构在这里:

替代文字

现在我有一条记录:

替代文字

我想阅读并打印此表的内容为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>
Run Code Online (Sandbox Code Playgroud)

但是我得到了???????? 结果:这是输出: 替代文字

我的错误在哪里?

sha*_*mar 25

总是获得正确编码的UTF-8文本的四个好步骤:

1)在任何其他查询之前运行此查询:

 mysql_query("set names 'utf8'");
Run Code Online (Sandbox Code Playgroud)

2)将其添加到您的HTML头:

 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
Run Code Online (Sandbox Code Playgroud)

3)在PHP代码的顶部添加:

 header("Content-Type: text/html;charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)

4)UTF-8 without BOM使用Notepad ++或任何其他优秀的文本编辑器/ IDE,使用编码保存文件.


Har*_*Das 10

将字符集设置为utf8,如下所示:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
Run Code Online (Sandbox Code Playgroud)


Pek*_*ica 6

您没有将HTML页面定义为UTF-8.有关如何做到这一点,请参阅此问题.

可能还需要将数据库连接显式设置为UTF8.做一个

mysql_query("SET NAMES utf8;");
Run Code Online (Sandbox Code Playgroud)

^将其放在数据库连接脚本下或包含并确保在执行任何必要的查询之前放置它.另外,对于搭配,请花点时间确保你设置正确的语法类型和general_ci似乎对我有用.作为结局,在敲击头部后清除缓存,将浏览器设置为正确的编码工具栏 - >视图 - >编码

建立连接后将连接设置为UTF8可以解决问题.如果第一步已经有效,请不要这样做.