Home » Php » Problems displaying Japanese characters using PHP and MySQL

Problems displaying Japanese characters using PHP and MySQL

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&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);
}

Answer:

If you are using objects for db access, then here’s the 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');
    }
}