PHP及网页使用UTF-8编码,数据库是sql server2008,使用默认编码(936,即GBK编码)
当读取数据库数据时,使用php自带的json_encode()返回到前端,结果中文不显示。
解决办法:
<?<span>php </span><span>header</span>("Content-Type: text/html;charset=utf-8"<span>); </span><span>//</span><span>告诉浏览器不要缓存数据</span> <span>header</span>("Cache-Control: no-cache"<span>); </span><span>require</span> "../conn.php"<span>; </span><span>require</span> "../share/json_gbk2utf8.php"<span>; </span><span>$query</span> = '<span>SELECT seq, employeeID, employeeName, department, position, sex, birthday, entryTime, description, convert(varchar(20),createTime,120) as createTime,<span>; </span><span>$arr</span> = <span>Array</span><span>(); </span><span>$query</span> = <span>iconv</span>("utf-8", "gbk//ignore", <span>$query</span>);<span>//</span><span>为了解决中文乱码问题 </span> <span>if</span>(<span>$result</span> = sqlsrv_query(<span>$conn</span>, <span>$query</span><span>)){ </span><span>while</span>(<span>$row</span> = sqlsrv_fetch_array(<span>$result</span><span>)){ </span><span>$arr</span>[] = <span>$row</span><span>; } } </span><span>$data</span> = JSON(<span>$arr</span><span>); </span><span>//</span><span> file_put_contents("E:/mylog.log", "JSON:".$data."\r\n", FILE_APPEND); </span> <span>echo</span> <span>$data</span><span>; </span>?>
<?<span>php </span><span>//</span><span>json_gbk2utf8.php</span><span> /*</span><span>************************************************************* *为了实现对含有中文字符的数组进行json编码 * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @access public * ************************************************************</span><span>*/</span> <span>function</span> arrayRecursive(&<span>$array</span>, <span>$function</span>, <span>$apply_to_keys_also</span> = <span>false</span><span>) { </span><span>static</span> <span>$recursive_counter</span> = 0<span>; </span><span>if</span> (++<span>$recursive_counter</span> > 1000<span>) { </span><span>die</span>('possible deep recursion attack'<span>); } </span><span>foreach</span> (<span>$array</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) { </span><span>if</span> (<span>is_array</span>(<span>$value</span><span>)) { arrayRecursive(</span><span>$array</span>[<span>$key</span>], <span>$function</span>, <span>$apply_to_keys_also</span><span>); } </span><span>else</span><span> { </span><span>//</span><span> file_put_contents("E:/mylog.log", "原始:".$value."\r\n", FILE_APPEND);</span> <span>$value</span> = <span>iconv</span>("gbk//ignore", "utf-8", <span>$value</span><span>); </span><span>//</span><span> file_put_contents("E:/mylog.log", "utf-8:".$value."\r\n", FILE_APPEND);</span> <span>$array</span>[<span>$key</span>] = <span>$function</span>(<span>$value</span><span>); </span><span>//</span><span> file_put_contents("E:/mylog.log", "urlencode:".$array[$key]."\r\n", FILE_APPEND);</span> <span> } </span><span>if</span> (<span>$apply_to_keys_also</span> && <span>is_string</span>(<span>$key</span><span>)) { </span><span>$new_key</span> = <span>$function</span>(<span>$key</span><span>); </span><span>if</span> (<span>$new_key</span> != <span>$key</span><span>) { </span><span>$array</span>[<span>$new_key</span>] = <span>$array</span>[<span>$key</span><span>]; </span><span>unset</span>(<span>$array</span>[<span>$key</span><span>]); } } } </span><span>$recursive_counter</span>--<span>; } </span><span>/*</span><span>************************************************************* * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * ************************************************************</span><span>*/</span> <span>function</span> JSON(<span>$array</span><span>) { arrayRecursive(</span><span>$array</span>, 'urlencode', <span>true</span><span>); </span><span>$json</span> = json_encode(<span>$array</span><span>); </span><span>return</span> <span>urldecode</span>(<span>$json</span><span>); } </span><span>/*</span><span> $array = array ( 'Name'=>'希亚', 'Age'=>20 ); echo JSON($array); </span><span>*/</span> ?>
这样,sql server 2008中的中文就可以在网页正常显示了。
如果要将中文正常插入到sql server 2008中,还要加入一条代码:$query = iconv("utf-8", "gbk//ignore", $query);//为了解决中文乱码问题
完整代码如下 :
<?<span>php </span><span>/*</span><span>* * 如果员工编号在MySql中不存在则在MySql中插入员工记录 * 如果该员工编号已经存在则进行更新操作 </span><span>*/</span> <span>//</span><span>如果用JSON格式则要使用text/html,不能使用text/xml</span> <span>header</span>("Content-Type: text/html;charset=utf-8"<span>); </span><span>//</span><span> header("Content-Type: text/html;charset=GBK"); //告诉浏览器不要缓存数据</span> <span>header</span>("Cache-Control: no-cache"<span>); </span><span>require</span> '../conn.php'<span>; </span><span>$seq</span> = <span>$_POST</span>["seq"<span>]; </span><span>$employeeID</span> = <span>$_POST</span>["employeeID"<span>]; </span><span>$employeeName</span> = <span>$_POST</span>["employeeName"<span>]; </span><span>$department</span> = <span>$_POST</span>["department"<span>]; </span><span>if</span>(!<span>isset</span>(<span>$seq</span>) || <span>$seq</span> == ""){<span>//</span><span>seq不存在则插入新记录</span> <span>$query</span> = "<span>INSERT INTO employees (employeeID, employeeName, department, createTime, updateTime) VALUES (N'</span><span>$employeeID</span>',N'<span>$employeeName</span>',N'<span>$department</span><span>', getdate(), getdate())</span>"<span>; }</span><span>else</span>{<span>//</span><span>如果seq已存在则更新已有记录</span> <span>$query</span> = "UPDATE employees SET employeeID='<span>$employeeID</span><span>', employeeName='</span><span>$employeeName</span>',department='<span>$department</span><span>', updateTime=getdate() WHERE seq='</span><span>$seq</span>'"<span>; } </span><span>//</span><span> file_put_contents("E:/mylog.log", $query."\r\n",FILE_APPEND);//用于调试</span> <span>$query</span> = <span>iconv</span>("utf-8", "gbk//ignore", <span>$query</span>);<span>//</span><span>为了解决中文乱码问题</span></span> <span>if</span>(<span>$result</span> = sqlsrv_query(<span>$conn</span>, <span>$query</span><span>)){ </span><span>echo</span> <span>true</span><span>; }</span><span>else</span><span>{ </span><span>echo</span> <span>false</span><span>; } </span><span>//</span><span> echo $query;</span> ?>