Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems displaying Japanese characters using PHP and MySQL

Tags:

php

mysql

xml

utf-8

I am using PHP connecting to an MySQL database to create an XML file. Everything is working except for the character encoding. I need both Japanese and English characters so obviously I have chose to use UTF-8. Only problem is the Japanese characters from the database don't display correctly.

The collation on the database and tables is set to UTF8_general_ci, as is the MySQL connection collation.

My php file defines to use UTF-8 (and is saved in UTF-8 without BOM) in 2 different place, once in the header with the following line: header("Content-type: text/xml;charset=utf-8"); The other place it defines it is in the XML output file.

As a test I have had the php file write some Japanese characters just from within the code, so doesn't come from the database. This displays correctly (can be seen here http://jlearn.0sites.net/Flash/xml/xml.php ... the last 5 entries have some Japanese followed by question marks due to the Japanese that is meant to come from the database).

So the problem is most likely the database but everything looks correct to me.

Any ideas?

like image 336
Musera Avatar asked Jan 19 '11 22:01

Musera


1 Answers

Actually just posted this - php mysql query encoding problem


What I tend to find solves things a lot is;

mysql_query("SET NAMES 'utf8'");

Before any queries are performed.

The documentation recommends that you use mysql_set_charset but I often see that function missing.

if( function_exists('mysql_set_charset') ){
    mysql_set_charset('utf8', $db_con);
}else{
    mysql_query("SET NAMES 'utf8'", $db_con);
}
like image 184
Matt Lowden Avatar answered Sep 22 '22 12:09

Matt Lowden