json_encode 从 MSSQL-SELECT 产生 JSON_ERROR_UTF8

我对 mssql 的数据库查询有一个恼人的问题.如果结果包含像德语ä"这样的特殊字符,我将无法使用 json_encode 正确地将结果作为 json 获取.

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 result as json correctly.

json_last_error 返回 5 等于 JSON_ERROR_UTF8.我猜数据库不会返回 UTF-8 编码的值.数据库集合是*Latin1_General_CI_AS*,受影响的列是varchars.

json_last_error return 5 which is equal to JSON_ERROR_UTF8. I guess the database does not return the values as UTF-8 encoded. The database collection is *Latin1_General_CI_AS* and the affected columns are varchars.

php mssql.charset 配置没有效果.

The php mssql.charset configuration has no effect.

我读到 mysql 用户可以使用 mysql_query('SET CHARACTER SET utf8'); 正确编码返回值.我该怎么做才能正确获取 mssql 的值?

I read that mysql users could use mysql_query('SET CHARACTER SET utf8'); to encode the return values correctly. What can I do, to get the values correctly for mssql?

提示 - 我无法更改数据库中的任何内容.

推荐答案

在 JSON 编码之前,在字符串周围使用 utf8_encode().

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

相关文章