php 从mysql表中读取utf-8内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3682409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:41:03  来源:igfitidea点击:

reading utf-8 content from mysql table

phpmysqlutf-8

提问by Alireza

I have a mysql table with contents

我有一个包含内容的 mysql 表

the structure is here:

结构在这里:

alt text

替代文字

Now I have one record in it:

现在我有一个记录:

alt text

替代文字

I want to read and print the content of this table to html This is my code:

我想读取这个表的内容并将其打印到 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>

but I get ???????? as the results: here is the output: alt text

但我明白了???????结果:这里是输出: 替代文字

Where is my mistake?

我的错误在哪里?

回答by shamittomar

Four good steps to always get correctly encoded UTF-8 text:

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

1) Run this query before any other query:

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

 mysql_query("set names 'utf8'");

2) Add this to your HTML head:

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

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

3) Add this at top of your PHP code:

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

 header("Content-Type: text/html;charset=UTF-8");

4) Save your file with UTF-8 without BOMencoding using Notepad++or any other good text-editor / IDE.

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

回答by Hari Das

Set the charset as utf8as follows:

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

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

回答by Pekka

You are not defining your HTML page as UTF-8. See this questionon ways to do that.

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

You mayalso need to set your database connection explicitly to UTF8. Doing a

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

mysql_query("SET NAMES utf8;");

^ Put it right under your database connection script or include and MAKE sure you have it placed before you do any necessary queries. Also, for collocation please take the time to make sure your setting it for your proper syntax type and general_ci seems working good for me when used. As a finale, clear your cache after banging your head, set your browser to proper encoding toolbar->view->encoding

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

Setting the connection to UTF8 after establishing the connection takes care of the problem. Don't do this if the first step already works.

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

回答by mouayad

try this :

尝试这个 :

mysql_set_charset('utf8', $yourConnection);

回答by Danny C

Old ways have been deprecated. If you are using PHP > 5.0.5 and using mysqli the new syntaxis now:

旧方式已被弃用。如果您使用 PHP > 5.0.5 并使用 mysqli,则新语法现在是:

$connection->set_charset("utf8")

Where $connectionis a reference to your connection to the DB.

哪里$connection是对您与数据库的连接的引用。

回答by Gio Lac Cagliari

I tried several solutions but the only one that worked is that of Hari Dass:

我尝试了几种解决方案,但唯一有效的是 Hari Dass:

$conn->set_charset("utf8");

$conn->set_charset("utf8");

回答by Robert

UTF-8 content from MySQL table with PDO

带有 PDO 的 MySQL 表中的 UTF-8 内容

To correctly get latin characters and so on from a MySQL table with PDO,

要从带有 PDO 的 MySQL 表中正确获取拉丁字符等,

there is an hidden info coming from a "User Contributed Note" in the PHP manual website

PHP 手册网站中的“用户贡献注释”中有一个隐藏信息

(the crazy thing is that originally, that contribution was downvoted, now luckily turned to positive .. sometime some people need to got blamed)

(疯狂的是,最初,该贡献被否决了,现在幸运地变成了积极......有时有些人需要受到指责)

my credits credits go to this articlethat pulled the solution and probably made that "User Contributed Note" to turn positive

我的学分学分转到这篇文章该文章提出了解决方案,并且可能使“用户贡献的注释”变为正面

If you want to have a clean database connection with correct Unicode characters

如果您想使用正确的 Unicode 字符建立干净的数据库连接

    $this->dbh = new PDO(
    "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8", 
    DB_USER, 
    DB_PASS);