11

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?

Alireza
  • 5,444
  • 9
  • 38
  • 50

7 Answers7

27

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.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 1
    I was echoing information from table. You can use the function utf8_encode() too. It worked fine. http://php.net/manual/en/function.utf8-encode.php – chepe263 Oct 02 '13 at 22:27
16

Set the charset as utf8 as follows:

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
Hari Das
  • 10,145
  • 7
  • 62
  • 59
6

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

UTF-8 content from MySQL table with PDO

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

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

(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 article that 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

    $this->dbh = new PDO(
    "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8", 
    DB_USER, 
    DB_PASS);
Robert
  • 490
  • 1
  • 5
  • 17
0

try this :

mysql_set_charset('utf8', $yourConnection);
mouayad
  • 443
  • 3
  • 10
0

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

$connection->set_charset("utf8")

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

Danny C
  • 2,980
  • 2
  • 28
  • 20
0

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

$conn->set_charset("utf8");