0

I am running into a problem, that I managed to fix by simply trying different options, but I don't understand why it works, it does not make sense to me...

Here it is, I receive UTF8 encoded JSON from Facebook, my table is in utf8_general_ci and my DB connection is UTF8 encoded too.

If I don't do anything special, the string is inserted as

Fabién

but if I utf8_decode it before inserting it is inserted as

Fabién

When I call mb_detect_encoding on my string, it returns UTF8.

Can anyone explain why I have to use utf8_decode for it to work? Or better, what to do to fix my code so I don't have to call utf8_decode?

Thanks in advance.

Fabien Warniez
  • 2,731
  • 1
  • 21
  • 30

2 Answers2

1

If your database/table/field is utf8_general_ci and the string is UTF-8, you should not convert the string before insert it.

You should select from the database and before printing in HTML, you should convert from UTF-8 to Latin1 (iso-8859-1) using this code:

 mb_convert_encoding($selected_string, 'utf-8', 'iso-8859-1')
Leo
  • 11
  • 2
0

Found the problem, you have to execute the command "SET NAMES utf8" after establishing the connection to the DB with PDO. I am using an older version of PHP (5.3.6), and charset=utf8 is ignored in the connection string...

See this question: PHP PDO: charset, set names?

Community
  • 1
  • 1
Fabien Warniez
  • 2,731
  • 1
  • 21
  • 30