Reputation: 740
I am trying to insert Japanese characters and display them, but all I get is gibberish. I have set my database encoding to utf8_general_ci; All columns inherit from table.
The code I am using is:
$db = new Database;
$json = file_get_contents('php://input');
$obj = json_decode($json, TRUE);
$db->query("SET NAMES utf8");
$db->query("SET CHARACTER SET utf8");
$db->query("INSERT INTO `flash_cards`.`flash_card` (`kanji`) VALUES ('いく')");
$db->execute();
$db->query('select * from flash_card');
$rows = $db->resultset();
print_r('行く');
echo '行く';
print_r(json_encode($rows));
The page properly displays the kanji characters when I directly display it to the page, but the problem arises when I pull from the database. Which leads me to believe it's an encoding/decoding problem.
The output I receive is
行く行く[{"id":"1","kanji":"\u3044\u304f"},{"id":"2","kanji":"\u3044\u304f"}]
I am unsure what else I can do. I spent a lot of time trying to figure this out to no avail. I am well aware of several posts throughout stackoverflow, but I am still stuck.
Any help will greatly be appreciated.
Upvotes: 1
Views: 1506
Reputation: 815
The following should work only if your MySQL version is >= 5.6.
I don't have any Japanese character/database to test but the following suggest that CHARACTER SET utf8mb4 COLLATION utf8mb4_unicode_520_ci
may be better than SET CHARACTER SET utf8
.
Didn't read that much, sorry just discovered that you used json_encode()
to get your output.
If you want to get the characters with json_encode()
you should do the following:
print_r(json_encode($rows, JSON_UNESCAPED_UNICODE));
Upvotes: 1