0

I have a project developed in PHP 5.4.35 using CakePHP, When I insert some unicode string into database,

Example : Bệnh suy giảm trí nhớ

After insert into database, this is the database content of record :

Bệnh suy giảm trí nhớ

If I use php 5.4 it display perfectly on website :

Bệnh suy giảm trí nhớ

I also develop an api in json format for this project and the api response when I get this data is: B\u1ec7nh suy gi\u1ea3m tr\u00ed nh\u1edb When client (mobile client) get this data and display, it is displayed perfectly same as website.

But when I move to a hosting with php 5.3.23, it is displayed on website just like on database:

Bệnh suy giảm trí nhớ

And the api response : B\u00e1\u00bb\u2021nh suy gi\u00e1\u00ba\u00a3m tr\u00c3\u00ad nh\u00e1\u00bb\u203a It is dislayed : Bệnh suy giảm trí nhớ on mobile device (which displayed perfectly if I use php5.4 for api)

;

then I tried to use utf8_decode function of php after get it from database and this is result:

utf8_decode($record);

B��?nh suy giảm trí nh��?

I thought that maybe this string was encoded before inserted to mysql in php5.4 and then decoded right after get it from the database, but in php5.3 it keep the string when insert and get from database. and the encoder, decoder of utf8 in php 5.3 and 5.4 is different??? I guest.

I'm using cakephp 2.5.3 and it use PDO to insert and get data from database I've tried all the way to change charset, collate .... but it take no effect like : PDO -> exec ('SET names utf8'), PDO -> exec (' Set names utf8 collate utf8_unicode_ci'); .... I've also set the charset of header, html meta tags....

my database use utf8_unicode_ci collation in both environment no different in database version It's just I found no way to resolve the problem, so if anyone know about this please give me some solutions Thanks

  • "Looks like this" and arbitrary recoding attempts are insufficient to diagnose anything. Set the page charset, view the actual database contents, not just HTML output, and probe the string encoding before and after insertion. – mario Jan 06 '15 at 03:07
  • 1
    I'm sorry but I'm new to satckoverflow so I cannot post image to show all the problem. I've already set the charset of HTML page to utf8, I've also checked the string data before insertion, The database content I posted on the question : Bệnh suy giảm trí nhá»› It is the database content I copied from database The problem is not in HTML output only, I developed an api for this website and the json output for that api also get the same problem! I will edit the question for more detail. – Ho D. Chung Jan 06 '15 at 03:18
  • Do you use the same database config in both versions with same encoding? – Gerd K Jan 06 '15 at 09:45

1 Answers1

0

The problem is : I set encoding = utf8 in php5.3 hosting environment, but in php5.4 (developing environment) I commented it, therefore when I move database from develop environment to product env, this issue occur. Just set the same Encoding option in database config in 2 env, the code work perfectly