2

I found a lot of answers for my problem but nothing worked. I have Chinese characters in my MYSQL database and when I SELECT my field in php I get '???' instead of '我们的产品'. I was able to INSERT this characters from php to my MYSQL database.

With characters like 'éà' I have no problem to get them in PHP.

My database, table and field are encoded in utf8_unicode_ci. There is my php file(also encoded in utf-8) :

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<?php
$query = mysql_query("SELECT * FROM table");
$arr = mysql_fetch_array($query))
echo ($arr['field']);
?>

If I replace my code like this it prints 我们的产品 :

$query = mysql_query("SELECT '我们的产品'");
arr = mysql_fetch_array($query)
echo ($arr[0]);

I hope I gave enough information.

chokichoc
  • 23
  • 4
  • 1
    The mysql_* functions are deprecated. see http://docs.php.net/manual/en/mysqlinfo.api.choosing.php – VolkerK Jun 30 '15 at 14:44

1 Answers1

2

set charset as utf8 before making a mysql query, like this

mysql_connect('localhost','mysql_user','your_password');
mysql_select_db('my_db') or die( "Unable to select database");
mysql_set_charset('UTF8');
mysql_query('Your query here');
dav
  • 8,931
  • 15
  • 76
  • 140
  • 1
    @chokichoc, great, though I agree with VolkerK that mysql_* functions are deprecated, it would be better to switch to PDO: http://php.net/manual/en/book.pdo.php or mysqli: http://php.net/manual/en/book.mysqli.php – dav Jun 30 '15 at 14:50
  • 1
    For Chinese, utf8mb4 would be better. Some characters exist only in utf8mb4. – Rick James Jun 30 '15 at 19:15