tags:

views:

67

answers:

2

Hi

I have a mysql table with contents

the structure is here:

alt text

Now I have one record in it:

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: here is the output: alt text

Where is my mistake?

+1  A: 

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;");

after establishing the connection takes care of that. Don't do this if the first step already works.

Pekka
thanks man! you are great!
safaali
+2  A: 

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
thank you got it :D
safaali
You're welcome.
shamittomar
it is good to set not only names, but collation too.
Tomasz Kowalczyk