Home > Backend Development > PHP Tutorial > PHP读取mssql json数据中文乱码的解决办法_PHP

PHP读取mssql json数据中文乱码的解决办法_PHP

WBOY
Release: 2016-05-27 10:34:28
Original
821 people have browsed it

PHP及网页使用UTF-8编码,数据库是sql server2008,使用默认编码(936,即GBK编码)

当读取数据库数据时,使用php自带的json_encode()返回到前端,结果中文不显示。

解决办法如下:

这样,sql server 2008中的中文就可以在网页正常显示了。

如果要将中文正常插入到sql server 2008中,还要加入一条代码:$query = iconv("utf-8", "gbk//ignore", $query);//为了解决中文乱码问题

完整代码如下 :

<&#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;>
Copy after login

以上所述是小编给大家介绍的PHP读取mssql json数据中文乱码的解决办法,希望对大家有所帮助!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template