Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading utf-8 content from mysql table

Tags:

php

mysql

utf-8

I have a mysql table with contents

the structure is here:

alt text

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:

Where is my mistake?

like image 315
Alireza Avatar asked Sep 10 '10 05:09

Alireza


3 Answers

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

1) Run this query before any other query:

 mysql_query("set names 'utf8'");

2) Add this to your HTML head:

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

3) Add this at top of your PHP code:

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

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

like image 192
shamittomar Avatar answered Oct 23 '22 21:10

shamittomar


Set the charset as utf8 as follows:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
like image 28
Hari Das Avatar answered Oct 23 '22 21:10

Hari Das


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

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

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

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

like image 30
Pekka Avatar answered Oct 23 '22 21:10

Pekka