PHP read Mssql,json data Chinese garbled

Source: Internet
Author: User
Tags getdate urlencode

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:

<?PHPHeader("Content-type:text/html;charset=utf-8"); //tell the browser not to cache the data    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), createtime,120) as Createtime,<span style= "color: #FF0000;" >//here to note, because the mssql2008 datetimne type is with milliseconds, directly in front of the display may be problematic, so do a conversion </span> convert (varchar), Updatetim e,120) as UpdateTime from Employees‘; $arr=Array(); $query=Iconv("Utf-8", "Gbk//ignore",$query);//in order to solve the problem of Chinese garbled    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/************************************************************** * To implement JSON encoding for arrays containing Chinese characters * * Use a specific function to manipulate all elements in an array * @param string & $array The string to process * @param string $function function to execute * @return Boolean $ Apply_to_keys_also is also applied to the key * @access public**************************************************************/functionArrayrecursive (&$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 language compatible) * @param array $array to The converted array * @return the JSON string converted by String * @access PUBLIC**************************************************************/functionJSON ($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:

<?PHP/** * If the employee number does not exist in MySQL then insert the employee record in MySQL * update if the employee number already exists*/    //If you use text/html in JSON format, you cannot use Text/xml    Header("Content-type:text/html;charset=utf-8");//header ("CONTENT-TYPE:TEXT/HTML;CHARSET=GBK"); Tell the browser not to cache the data    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 does not exist insert new record        $query= "INSERT into Employees (EmployeeID, EmployeeName, Department, Createtime, UpdateTime) VALUES (N '$employeeID', N '$employeeName', N '$department', GETDATE (), GETDATE ())"; }Else{//Update an existing record if the SEQ already exists        $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);//For commissioning<span style= "color: #FF0000;" >$query=Iconv("Utf-8", "Gbk//ignore",$query);//to solve the problem of Chinese garbled </span>    if($result= Sqlsrv_query ($conn,$query)){        Echo true; }Else{        Echo false; }//echo $query;?>

PHP read Mssql,json data Chinese garbled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.