Solutions for PHP to read mssqljson data with Chinese garbled characters

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.