Home >Backend Development >PHP Tutorial >Solution to Chinese garbled Chinese characters in mssql json data read by PHP, _PHP tutorial

Solution to Chinese garbled Chinese characters in mssql json data read by PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:54:331219browse

Solution to Chinese garbled Chinese garbled data read by PHP from mssql json.

PHP and web pages use UTF-8 encoding. The database is sql server2008 and uses the default encoding (936, which is GBK encoding )

When reading database data, use json_encode() that comes with PHP to return to the front end, and the result will not be displayed in Chinese.

The solution is as follows:

In this way, Chinese in sql server 2008 can be displayed normally on the web page.

If you want to insert Chinese into sql server 2008 normally, you also need to add a code: $query = iconv("utf-8", "gbk//ignore", $query);//In order to solve the problem of Chinese garbled characters

The complete code is as follows:

<&#63;php 
/**
* 如果员工编号在MySql中不存在则在MySql中插入员工记录
* 如果该员工编号已经存在则进行更新操作
*/
//如果用JSON格式则要使用text/html,不能使用text/xml
header("Content-Type: text/html;charset=utf-8");
// header("Content-Type: text/html;charset=GBK");
//告诉浏览器不要缓存数据
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不存在则插入新记录
$query = "INSERT INTO employees (employeeID, employeeName, department, 
createTime, updateTime)
VALUES (N'$employeeID',N'$employeeName',N'$department', 
getdate(), getdate())";
}else{//如果seq已存在则更新已有记录
$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);//用于调试
<span style="color:#FF0000;">$query = iconv("utf-8", "gbk//ignore", $query);//为了解决中文乱码问题</span>
if($result = sqlsrv_query($conn, $query)){
echo true;
}else{
echo false;
}
// echo $query;
&#63;>

The above is the solution introduced by the editor to you for reading garbled Chinese characters in mssql json data with PHP. I hope it will be helpful to you!

Articles you may be interested in:

  • php adodb connects to mssql to solve the garbled problem
  • Discuss the solution to the Chinese garbled code of PHP JSON in detail
  • PHP json_encode Chinese Solution to garbled code problem
  • Share the solution to garbled code problem in thinkphp3 query mssql database
  • Solution to json_encode in php to handle gbk and gb2312 Chinese garbled code problem
  • json_encode UTF-8 in php A better solution to garbled Chinese characters
  • A solution to garbled characters in PHP query mssql
  • Detailed explanation of the PHP json_encode() function and the problem of Chinese garbled characters

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119975.htmlTechArticleSolution to Chinese garbled Chinese garbled data read by PHP from mssql json data. PHP and web pages use UTF-8 encoding, and the database is sql server2008, uses the default encoding (936, GBK encoding) when reading database data...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn