catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Encoding error when returning json with Chinese text #917

Closed shadowfish07 closed 4 years ago

shadowfish07 commented 4 years ago

Describe the bug When I use select to get json with Chinese text from my database,it has some encoding error so that the result is hard to use,just like this:

{"rcode":"00000","id":"1","title":"shadow\u7684\u5546\u5e97","value":"{\"gold\": 200, \"title\": \"\u793a\u4f8b\u5546\u54c1\", \"descripsion\": \"\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u5546\u54c1\"}","creater":"1","reg_time":null}

The unicode character in the title field is usually converted automatically, but the /" is a big problem! Then I tried to use mysqli to get the same text and it returns exactly the right result,which has no unicode chars at all.

Information

Detail Code

    $data = $db->select('shop_series', '*', [
        'id' => $shopid
    ])[0];
echo json_encode($result);

Expected output

{
    "rcode": "00000",
    "id": "1",
    "title": "shadow的商店",
    "value": "{"gold": 200, "title": "示例商品", "descripsion": "这是一个示例商品"}",
    "creater": "1",
    "reg_time": null
}
praine commented 3 years ago

This is not a medoo issue, it's a json_encode issue. You need to use the JSON_UNESCAPED_UNICODE flag in your json_encode statement.

More info here: https://www.php.net/manual/en/json.constants.php