Encoding-I used PHP to retrieve data from mysql and convert the data to Json. why is the question mark displayed in Chinese?

Source: Internet
Author: User
The above is the data in my data inventory, which adopts the utf8_bin encoding. at the beginning, utf8_unicode_ci is used, but still cannot be used. below is my PHP code {code ...} finally, the browser displays the urine {code ...} why is question mark displayed in Chinese? Unicode I want...


The above is the data in my data inventory, which adopts the utf8_bin encoding. at the beginning, utf8_unicode_ci is used, but it still does not work.
Below is my PHP code


  Id = $ row ["id"]; $ user-> name = $ row ["name"]; $ user-> address = $ row ["address"]; $ user-> phonenumber = $ row ["phonenumber"]; $ data [] = $ user;} if (function_exists ('MySQL _ set_charset ') = false) {mysql_query ("set names UTF8", $ data);} else {mysql_set_charset ('utf8', $ data);} $ response = array ('code' => 200, 'message' => 'success for request', 'data' => $ data,); echo urldecode (json_encode ($ response )); Mysql_free_result ($ response); // release mysql_close (); // Close the database?>

Finally, the browser displays the urine.

{    "code": 200,     "message": "success for request",     "data": [        {            "id": "100",             "name": "??",             "address": "????",             "phonenumber": "13212340001"        },         {            "id": "101",             "name": "??",             "address": "????",             "phonenumber": "13512340002"        },         {            "id": "100",             "name": "??",             "address": "????",             "phonenumber": "13212340001"        },         {            "id": "101",             "name": "??",             "address": "????",             "phonenumber": "13512340002"        },         {            "id": "0",             "name": "",             "address": "",             "phonenumber": "0"        },         {            "id": "103",             "name": "??",             "address": "????",             "phonenumber": "13512340005"        },         {            "id": "103",             "name": "??",             "address": "????",             "phonenumber": "13512340005"        }    ]}

Why is question mark displayed in Chinese? I want Unicode encoding.

Reply content:


The above is the data in my data inventory, which adopts the utf8_bin encoding. at the beginning, utf8_unicode_ci is used, but it still does not work.
Below is my PHP code


  Id = $ row ["id"]; $ user-> name = $ row ["name"]; $ user-> address = $ row ["address"]; $ user-> phonenumber = $ row ["phonenumber"]; $ data [] = $ user;} if (function_exists ('MySQL _ set_charset ') = false) {mysql_query ("set names UTF8", $ data);} else {mysql_set_charset ('utf8', $ data);} $ response = array ('code' => 200, 'message' => 'success for request', 'data' => $ data,); echo urldecode (json_encode ($ response )); Mysql_free_result ($ response); // release mysql_close (); // Close the database?>

Finally, the browser displays the urine.

{    "code": 200,     "message": "success for request",     "data": [        {            "id": "100",             "name": "??",             "address": "????",             "phonenumber": "13212340001"        },         {            "id": "101",             "name": "??",             "address": "????",             "phonenumber": "13512340002"        },         {            "id": "100",             "name": "??",             "address": "????",             "phonenumber": "13212340001"        },         {            "id": "101",             "name": "??",             "address": "????",             "phonenumber": "13512340002"        },         {            "id": "0",             "name": "",             "address": "",             "phonenumber": "0"        },         {            "id": "103",             "name": "??",             "address": "????",             "phonenumber": "13512340005"        },         {            "id": "103",             "name": "??",             "address": "????",             "phonenumber": "13512340005"        }    ]}

Why is question mark displayed in Chinese? I want Unicode encoding.

if (function_exists('mysql_set_charset') === false) {    mysql_query("SET NAMES UTF8",$data);}else{    mysql_set_charset('utf8',$data);}

This section should be placed inmysql_queryPreviously, the second parameter in the other two places should not be$dataBut it should be$connect

I have encountered this problem before. just add this question.mysqli_query($conn,"SET NAMES utf8");

class DBHelper{        public function DBHelper(){}        private static function getConn(){        $conn = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);        mysqli_query($conn,"SET NAMES utf8");        return $conn;    }        public static function opearting($sql){        return mysqli_query(self::getConn(),$sql);            }}

The Chinese urlencode encoding problem, which I encountered, cannot be decoded directly. Otherwise, garbled characters may occur. The method I use is.
Separate encoding. Then in decoding ,'/**

* [ReturnUrlencode uses recursion to traverse all arrays and parse them] * @ param [type] $ arr [description] */function ReturnUrlencode ($ arr) {foreach ($ arr as $ key =>$ value) {if (is_array ($ value) {$ arr [$ key] = ReturnUrlencode ($ value );} else {$ arr [$ key] = urlencode ($ value) ;}}return $ arr ;}' # In this way, the output json Chinese characters will not be garbled. Urldecode (json_encode (ReturnUrlencode ($ arr )));

if (function_exists('mysql_set_charset') === false) {    mysql_query("SET NAMES UTF8",$data);}else{    mysql_set_charset('utf8',$data);}

This sentence should be placed before traversing data.

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.