PHP and Web pages use UTF-8 encoding, the database is SQL server2008, using the default encoding (936, which is GBK encoding)
When reading the database data, use PHP's own json_encode () to return to the front end, the result Chinese is not displayed.
The solution is as follows:
In this way, the Chinese in SQL Server 2008 can be displayed correctly on the Web page.
If you want to insert Chinese correctly into SQL Server 2008, add a code: $query = Iconv ("Utf-8", "Gbk//ignore", $query);//In order to solve the problem of Chinese garbled
The complete code is as follows:
<?php/*** If the employee number does not exist in MySQL then insert the employee record in MySQL * If the employee number already exists then update the operation *///if you want to use text/html in JSON format, you cannot use 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 = = "") {//SEQ does not exist insert new record $query = "INSERT INTO employees (EMPLOYEEI D, EmployeeName, Department, Createtime, UpdateTime) VALUES (n ' $employeeID ', n ' $employeeName ', n ' $department ', GETDATE ( ), GETDATE ()) ";} else{//Update existing record if seq already exists $query = "Update Employees SET employeeid= ' $employeeID ', employeename= ' $employeeName ', Department= ' $department ', updatetime=getdate () WHERE seq= ' $seq ' ";} File_put_contents ("E:/mylog.log", $query. " \ r \ n ", file_append);//For debugging <span style=" color: #FF0000; " > $query = Iconv ("Utf-8", "Gbk//ignore", $query);//In order to solve the problem of Chinese garbled </span>if ($result = sqlsrv_query ($conn, $query)) {echo true;} Else{echo false;} Echo $query;? >
The above is a small series to introduce PHP read MSSQL JSON data Chinese garbled solution, we hope to help!