Problems displaying Japanese characters using PHP and MySQL

I am using a PHP connection to a MySQL database to create an XML file. Everything works except character encoding. I need both Japanese and English characters, so obviously I decided to use UTF-8. Only problem is that Japanese characters from the database are not displayed correctly.

The database and table settings are set to UTF8_general_ci, as well as the mapping of MySQL connections.

My php file defines the use of UTF-8 (and is stored in UTF-8 without specification) in 2 different places, once in the header with the following line: header ("Content-type: text / xml; charset = utf-8"); Another place that it defines is in the XML output file.

As a test, I had a php file that some Japanese characters write only from code, so it does not come from the database. This displays correctly (can be seen here http://jlearn.0sites.net/Flash/xml/xml.php ... the last 5 entries contain some Japanese followed by question marks because of the Japanese that should appear from the database )

So, the problem is most likely in the database, but everything looks right to me.

Any ideas?

+7
source share
2 answers

Actually just posted this - mysql php request encoding problem


What I usually look for solves a lot:

mysql_query("SET NAMES 'utf8'"); 

Before performing any queries.

The documentation recommends using mysql_set_charset , but I often see that the function is missing.

 if( function_exists('mysql_set_charset') ){ mysql_set_charset('utf8', $db_con); }else{ mysql_query("SET NAMES 'utf8'", $db_con); } 
+12
source

If you use objects to access db, then here is a solution that works for me

 class DB { public $Link; private static $_instance; public static function instance() { if (NULL === self::$_instance) { self::$_instance = new DB; } return self::$_instance; } private function __construct() { $host = 'localhost'; $user = 'your_user_name'; $pass = 'your_password; $db = 'db_name'; $this->Link = new mysqli($host, $user, $pass, $db); $this->Link->set_charset('utf8'); } } 
0
source

All Articles