簡體   English   中英

從mysql表中讀取utf-8內容

[英]reading utf-8 content from mysql table

我有一個包含內容的 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>

但我明白了??????? 結果:這里是輸出:替代文字

我的錯誤在哪里?

始終獲得正確編碼的 UTF-8 文本的四個好步驟:

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

 mysql_query("set names 'utf8'");

2)將此添加到您的HTML頭:

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

3) 在 PHP 代碼的頂部添加:

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

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

將字符集設置為utf8 ,如下所示:

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

您沒有將 HTML 頁面定義為 UTF-8。 關於如何做到這一點,請參閱此問題

可能還需要將數據庫連接顯式設置為 UTF8。 做一個

mysql_query("SET NAMES utf8;");

^ 將它放在您的數據庫連接腳本下或包含並確保在您進行任何必要的查詢之前已將其放置。 此外,對於搭配,請花時間確保您將其設置為正確的語法類型,並且在使用時 general_ci 似乎對我有用。 最后,敲擊頭部后清除緩存,將瀏覽器設置為正確的編碼工具欄->視圖->編碼

在建立連接后將連接設置為 UTF8 可以解決這個問題。 如果第一步已經奏效,請不要這樣做。

試試這個:

mysql_set_charset('utf8', $yourConnection);

舊方式已被棄用。 如果您使用 PHP > 5.0.5 並使用 mysqli,則新語法現在是:

$connection->set_charset("utf8")

其中$connection是對您與數據庫的$connection的引用。

我嘗試了幾種解決方案,但唯一有效的是 Hari Dass:

$conn->set_charset("utf8");

帶有 PDO 的 MySQL 表中的 UTF-8 內容

要從帶有 PDO 的 MySQL 表中正確獲取拉丁字符等,

PHP 手冊網站中的“用戶貢獻注釋”中有一個隱藏信息

(瘋狂的是,最初,該貢獻被否決了,現在幸運地變成了積極......有時有些人需要受到指責)

我的學分學分轉到這篇文章該文章提出了解決方案,並且可能使“用戶貢獻的注釋”變為正面

如果您想使用正確的 Unicode 字符建立干凈的數據庫連接

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM