PHP and web pages using UTF-8 encoding, the database is sqlserver2008, using the default encoding, when reading database data, use php built-in json_encode () returned to the front-end, the results of Chinese is not displayed. Below small make up to introduce PHP read mssqljson data Chinese garbled PHP and web page using UTF-8 encoding, database is SQL server2008, using the default encoding (936, that is, GBK encoding)
When reading database data, use the json_encode () provided by php to return to the front-end. The results are not displayed in Chinese.
The solution is as follows:
In this way, the Chinese characters in SQL server 2008 can be displayed on the webpage normally.
If you want to insert Chinese characters to SQL server 2008 normally, add the following code: $ query = iconv ("UTF-8", "gbk // ignore", $ query ); // to solve Chinese garbled characters
The complete code is as follows:
<? Php/*** if the employee number does not exist in MySql, insert the employee record in MySql * if the employee number already exists, perform the update operation * // if the employee number is in JSON format use text/html, text/xmlheader ("Content-Type: text/html; charset = utf-8"); // header ("Content-Type: text/html; charset = GBK "); // tell the browser not to Cache the data header ("cache-Control: no-Cache"); require '.. /conn. php '; $ seq = $ _ POST ["seq"]; $ employeeID = $ _ POST ["employeeID"]; $ employeeName = $ _ POST ["employeeName"]; $ department = $ _ POST ["department"]; if (! Isset ($ seq) | $ seq = "") {// if seq does not exist, INSERT the new record $ query = "insert into employees (employeeID, employeeName, department, createTime, updateTime) VALUES (n' $ employeeID ', n' $ employeeName', n' $ department ', getdate (), getdate ())";} else {// if seq already exists, UPDATE the existing record $ query = "UPDATE employees SET employeeID = '$ employeeID', employeeName = '$ employeeName', department = '$ department ', updateTime = getdate () WHERE seq = '$ seq' ";} // file_put_c Ontents ("E:/mylog. log ", $ query. "\ r \ n", FILE_APPEND); // used for debugging $ query = iconv ("UTF-8", "gbk // ignore", $ query ); // to solve Chinese garbled characters, if ($ result = sqlsrv_query ($ conn, $ query) {echo true;} else {echo false;} // echo $ query;?>
The above is a small series of solutions for PHP to read mssql json data with Chinese garbled characters. I hope to help you!