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 ?????
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'
));