0

How do I handle Scandinavian characters when using MySQLi? Text Päivänkakkara is now printed as P\u00e4iv\u00e4nkakkara in the browser.

I'm using the set_charset function like below when initializing the database connection:

$conn->set_charset("utf8");

The collation of the table is utf8_unicode_ci.

mkkekkonen
  • 1,704
  • 2
  • 18
  • 35
  • Put this in your head tag `` – Mihai Sep 26 '17 at 18:58
  • All it takes, is one wrong charset setting in your application - *everything* needs to be the same charset! I have previously written [**an answer about UTF-8 encoding**](https://stackoverflow.com/a/31899827/4535200) that contains a little checklist, that will cover *most* of the charset issues in a PHP/MySQL application. There's also a more in-depth topic, [**UTF-8 All the Way Through**](https://stackoverflow.com/q/279170/4535200). Most likely, you'll find a solution in either one or both of these topics. – Qirel Sep 26 '17 at 19:01
  • 1
    `P\u00e4iv\u00e4nkakkara` looks like you're encoding of sorts (that's unicode), do you have `json_encode()` somewhere? – Qirel Sep 26 '17 at 19:02
  • Yes, I'm using `json_encode`. Do I have to manually decode the JSON (I'm using PHP via AJAX)? – mkkekkonen Sep 26 '17 at 19:40

1 Answers1

0

I managed to get the text to print correctly by using

echo json_encode($foo, JSON_UNESCAPED_UNICODE);

Then, however, I have to explicitly parse the JSON client-side. My client is written in TypeScript, so:

JSON.parse(data);
mkkekkonen
  • 1,704
  • 2
  • 18
  • 35