PHP及網頁使用UTF-8編碼,數據庫是sql server2008,使用默認編碼(936,即GBK編碼)
當讀取數據庫數據時,使用php自帶的json_encode()返回到前端,結果中文不顯示。
解決辦法:
employeeGet.php <?php header("Content-Type: text/html;charset=utf-8"); //告訴浏覽器不要緩存數據 header("Cache-Control: no-cache"); require "../conn.php"; require "../share/json_gbk2utf8.php"; $query = 'SELECT seq, employeeID, employeeName, department, position, sex, birthday, entryTime, description, convert(varchar(20),createTime,120) as createTime,<SPAN style="COLOR: #ff0000">//這裡要注意,因為mssql2008的datetimne類型是帶有毫秒的,直接在前端顯示 可能會有問題,所以要做一次轉換</SPAN> convert(varchar(20),updateTime,120) as updateTime FROM employees'; $arr = Array(); $query = iconv("utf-8", "gbk//ignore", $query);//為了解決中文亂碼問題 if($result = sqlsrv_query($conn, $query)){ while($row = sqlsrv_fetch_array($result)){ $arr[] = $row; } } $data = JSON($arr); // file_put_contents("E:/mylog.log", "JSON:".$data."\r\n", FILE_APPEND); echo $data; ?> employeeGet.php <?php header("Content-Type: text/html;charset=utf-8"); //告訴浏覽器不要緩存數據 header("Cache-Control: no-cache"); require "../conn.php"; require "../share/json_gbk2utf8.php"; $query = 'SELECT seq, employeeID, employeeName, department, position, sex, birthday, entryTime, description, convert(varchar(20),createTime,120) as createTime,//這裡要注意,因為mssql2008的datetimne類型是帶有毫秒的,直接在前端顯示 可能會有問題,所以要做一次轉換 convert(varchar(20),updateTime,120) as updateTime FROM employees'; $arr = Array(); $query = iconv("utf-8", "gbk//ignore", $query);//為了解決中文亂碼問題 if($result = sqlsrv_query($conn, $query)){ while($row = sqlsrv_fetch_array($result)){ $arr[] = $row; } } $data = JSON($arr); // file_put_contents("E:/mylog.log", "JSON:".$data."\r\n", FILE_APPEND); echo $data; ?>
?<?php
//json_gbk2utf8.php
/**************************************************************
*為了實現對含有中文字符的數組進行json編碼
*
* 使用特定function對數組中所有元素做處理
* @param string &$array 要處理的字符串
* @param string $function 要執行的函數
* @return boolean $apply_to_keys_also 是否也應用到key上
* @access public
*
*************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
// file_put_contents("E:/mylog.log", "原始:".$value."\r\n", FILE_APPEND);
$value = iconv("gbk//ignore", "utf-8", $value);
// file_put_contents("E:/mylog.log", "utf-8:".$value."\r\n", FILE_APPEND);
$array[$key] = $function($value);
// file_put_contents("E:/mylog.log", "urlencode:".$array[$key]."\r\n", FILE_APPEND);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
/**************************************************************
*
* 將數組轉換為JSON字符串(兼容中文)
* @param array $array 要轉換的數組
* @return string 轉換得到的json字符串
* @access public
*
*************************************************************/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
/*
$array = array
(
'Name'=>'希亞',
'Age'=>20
);
echo JSON($array);
*/
?>
<?php
//json_gbk2utf8.php
/**************************************************************
*為了實現對含有中文字符的數組進行json編碼
*
* 使用特定function對數組中所有元素做處理
* @param string &$array 要處理的字符串
* @param string $function 要執行的函數
* @return boolean $apply_to_keys_also 是否也應用到key上
* @access public
*
*************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
arrayRecursive($array[$key], $function, $apply_to_keys_also);
} else {
// file_put_contents("E:/mylog.log", "原始:".$value."\r\n", FILE_APPEND);
$value = iconv("gbk//ignore", "utf-8", $value);
// file_put_contents("E:/mylog.log", "utf-8:".$value."\r\n", FILE_APPEND);
$array[$key] = $function($value);
// file_put_contents("E:/mylog.log", "urlencode:".$array[$key]."\r\n", FILE_APPEND);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
/**************************************************************
*
* 將數組轉換為JSON字符串(兼容中文)
* @param array $array 要轉換的數組
* @return string 轉換得到的json字符串
* @access public
*
*************************************************************/
function JSON($array) {
arrayRecursive($array, 'urlencode', true);
$json = json_encode($array);
return urldecode($json);
}
/*
$array = array
(
'Name'=>'希亞',
'Age'=>20
);
echo JSON($array);
*/
這樣,sql server 2008中的中文就可以在網頁正常顯示了。
如果要將中文正常插入到sql server 2008中,還要加入一條代碼:$query = iconv("utf-8", "gbk//ignore", $query);//為了解決中文亂碼問題
完整代碼如下 :
<?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; ?> <?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);//用於調試 $query = iconv("utf-8", "gbk//ignore", $query);//為了解決中文亂碼問題 if($result = sqlsrv_query($conn, $query)){ echo true; }else{ echo false; } // echo $query; ?>