从mysql表读取utf-8内容 [英] reading utf-8 content from mysql table

查看:101
本文介绍了从mysql表读取utf-8内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含内容的mysql表

I have a mysql table with contents

结构在这里:

现在我有一条记录:

我想读取此表的内容并将其打印为html 这是我的代码:

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

<?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:

我的错误在哪里?

推荐答案

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

1)先运行此查询,再执行其他任何查询:

1) Run this query before any other query:

 mysql_query("set names 'utf8'");

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

2) Add this to your HTML head:

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

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

3) Add this at top of your PHP code:

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

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

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

这篇关于从mysql表读取utf-8内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆