問題描述
我對 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.
這篇關于json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!