Question

Last couple of hours i have tried hard but failed to come into a solution. why it's happening.... I have a table with collation=utf8_general_ci and it's column/field collation is also set to utf8_general_ci. i can see thai data properly from phpmyadmin. but when i fetch this table data and show using php code it shows ???????

click to have a look the page.

in this page i have written one thai word directly which shows properly but same word/text when i fetch from database and display it shows ???

i am using

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

currently and when i have tried with

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

i found Direct and from Database both text/word display as ?????

Was it helpful?

Solution

It may be possible that your MySQL connection itself is not using utf8. From the MySQL manual:

SET NAMES indicates what character set the client will use to send SQL statements 
to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming 
messages from this client are in character set cp1251.” It also specifies the 
character set that the server should use for sending results back to the client. 
(For example, it indicates what character set to use for column values if you use 
a SELECT statement.)

Depending on how you are connecting to the database (mysql_connect/mysqli_connect or PDO) the steps are a little bit different. If using mysql_connect(); or mysqli_connect; then you will need to run a mysql_query("SET NAMES utf8");. In thoery you can use the same steps if using PHP PDO but you can alternatively set the init command during PDO object construction. Here's an example from a database interaction class of mine.

$dsn = 'mysql:host='.$database_detail['dbhost'].';dbname='.$database_detail['dbname'];
$this->dbh = new PDO($dsn, $database_detail['dbuser'],
    $database_detail['dbpass'], array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => FALSE,
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
));

OTHER TIPS

I have this problem with Spanish sometimes, and even though I set the content type in html it still displays the wrong encoding type.

What works for me is if I "save the php document" with the correct formatting and then that fixes the issue.

You see when your document gets severed from Apache, or whatever your using, it sends the document type that it reads, but your PHP code is dynamically getting the content from the database.

So I would do the following:

Write the thai word in HTML and get it to display correctly. Try doing this by saving the document in different encoding options. This is normally the option in the save dialog, and once you have it displaying then try fetching it from the Database.

Of course, you could force Apache to serve documents in the encoding you want, but this assumes that you have access to those settings.

I hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top