json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT

前端 未结 2 2021
孤街浪徒
孤街浪徒 2020-12-04 01:45

I have an annoying problem with a database query to mssql. If the result contains special characters like the german \'ä\', I cannot use json_encode to get the resu

相关标签:
2条回答
  • 2020-12-04 02:19

    Before you JSON encode, use utf8_encode() around the string.

    0 讨论(0)
  • 2020-12-04 02:29

    You can just set this in your connection also:

    $result = sqlsrv_connect($hostname, array(
        'UID' => $username,
        'PWD' => $password,
        'Database' => $database,
        "CharacterSet" => "UTF-8"   // <---- here the magic happens 
    ));
    

    For me, this is the easer way.

    0 讨论(0)
提交回复
热议问题