代码之家  ›  专栏  ›  技术社区  ›  Alireza

从mysql表读取utf-8内容

  •  10
  • Alireza  · 技术社区  · 16 年前

    结构如下:

    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

    5 回复  |  直到 16 年前
        1
  •  25
  •   shamittomar    16 年前

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

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

     mysql_query("set names 'utf8'");
    

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

    3) 在PHP代码顶部添加以下内容:

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

    4) 将文件保存为 UTF-8 without BOM 编码使用 Notepad++

        2
  •  13
  •   Hari Das    9 年前

    将字符集设置为 utf8型 具体如下:

    $conn = new mysqli($servername, $username, $password, $dbname);
    $conn->set_charset("utf8");
    
        3
  •  6
  •   Community Mohan Dere    9 年前

    this question 想办法做到这一点。

    你呢 可以

    mysql_query("SET NAMES utf8;");
    

    ^ 在执行任何必要的查询之前,请将它放在数据库连接脚本或include下面,并确保已将其放置好。另外,对于搭配请花点时间确定你的

        4
  •  0
  •   mouayad    11 年前

    试试这个:

    mysql_set_charset('utf8', $yourConnection);
    
        5
  •  0
  •   Danny C    9 年前

    旧方法已被弃用。如果您使用的是PHP>5.0.5和mysqli new syntax 现在是:

    $connection->set_charset("utf8")
    

    在哪里? $connection 是指您与数据库的连接。

        6
  •  0
  •   Gio Lac Cagliari    7 年前

    我试过几种解决办法,但只有一种有效 是哈里达斯的作品:

    $conn->设置字符集(“utf8”);

        7
  •  0
  •   Robert    6 年前

    使用PDO从MySQL表获取UTF-8内容

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

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

    我的学分是 this article 这就解决了问题,而且很可能 User Contributed Note “转正

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

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