0

I have some json I need to encode - Vietnamese word. I try use JSON_UNESCAPED_UNICODE but look like it's not working anymore.

  header("Content-type: application/json; charset=utf-8");
  $db = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysqli_connect_errno());
  $result = mysqli_query($db,"SELECT * FROM categories") ;
  if (mysqli_num_rows($result) > 0) {
    $response["categories"] = array();
    while ($row = mysqli_fetch_array($result)) {
       $categories["id"] = json_encode($row["id"]);
       $categories["name"]= json_encode($row["name"],JSON_UNESCAPED_UNICODE);
       array_push($response["categories"], $categories);
    }
    $response["result"] = "OK";
    $myJSON = json_encode($response);
    echo $myJSON;
  }

Input:

id=1 name=Truyện Cổ Tích

Output:

id= 1 name = false

It's my first time i try PHP. My php version is 7.2.8.

TheZero
  • 260
  • 2
  • 4
  • 13
  • Possible duplicate of [How to display Unicode data with PHP](https://stackoverflow.com/questions/2446778/how-to-display-unicode-data-with-php) – scrowler Aug 20 '18 at 03:49
  • @RobbieAverill Thanks to mentions me. I seen that. But not work with me. – TheZero Aug 20 '18 at 04:12

2 Answers2

0
  1. first run mysqli_query('SET NAMES utf8').
  2. print the error info by json_last_error()
  • it's nothing happened. Not show any error. Json string print fine, but still get false with "Truyện Cổ Tích" and get "Truy?n Th?n Tho?i" with "Truyện Thần Thoại" – TheZero Aug 20 '18 at 04:08
0

You must config connect SQL that it can encode set mysqli_set_charset($con,"utf8");