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.
Workaround:
employeeget.php
It is important to note that because the Datetimne type of mssql2008 is with milliseconds, it may be problematic to display it directly in the front-end, so do a conversion CONVERT (varchar (), updatetime,120) as updatetime from employees '; $arr = Array (); $query = Iconv ("Utf-8", "Gbk//ignore", $query),//In order to solve the Chinese garbled problem 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
? json_gbk2utf8.php
/**************************************************************
* To implement JSON encoding of arrays containing Chinese characters
*
* Processing of all elements in an array using a specific function
* @param string & $array strings to be processed
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also is also applied to the 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", "Original:". $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--;
}
/**************************************************************
*
* Convert Array to JSON string (Chinese compatible)
* @param array $array to convert
* JSON string to be converted @return string
* @access Public
*
*************************************************************/
function JSON ($array) {
Arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}
/*
$array = array
(
' Name ' = ' Shia ',
' Age ' =>20
);
echo JSON ($array);
*/
?>
json_gbk2utf8.php
/**************************************************************
* To implement JSON encoding of arrays containing Chinese characters
*
* Processing of all elements in an array using a specific function
* @param string & $array strings to be processed
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also is also applied to the 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", "Original:". $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--;
}
/**************************************************************
*
* Convert Array to JSON string (Chinese compatible)
* @param array $array to convert
* JSON string to be converted @return string
* @access Public
*
*************************************************************/
function JSON ($array) {
Arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}
/*
$array = array
(
' Name ' = ' Shia ',
' Age ' =>20
);
echo JSON ($array);
*/
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:
$query = Iconv ("Utf-8", "Gbk//ignore", $query);//In order to solve the problem of Chinese garbledif ($result = sqlsrv_query ($conn, $query)) {echo true; }else{echo false; }//Echo $query; ?>
http://www.bkjia.com/PHPjc/477223.html www.bkjia.com true http://www.bkjia.com/PHPjc/477223.html techarticle PHP and Web pages using UTF-8 encoding, the database is SQL server2008, using the default encoding (936, that is, GBK encoding) when reading database data, using PHP's own Json_encode () back to the front end, knot ...