Why does the PHP processing database read the "Zhang San" encoding instead of the kanji?

Source: Internet
Author: User
$con = mysql_connect ("localhost", "root", "1234");
mysql_select_db ("Test", $con);

mysql_query ("Set names UTF8");
mysql_query ("Set Character_set_client=utf8");
mysql_query ("Set Character_set_results=utf8");


$q =mysql_query ("SELECT * from people");
while ($e =mysql_fetch_assoc ($q)) {
$output []= $e; }
Print (Json_encode ($output));
Mysql_close ();
?>



Output Result:
[{"id": "Ten", "Name": "John", "Sex": "1", "Birthyear": "1979"},{"id": "One", "Name:" \u5f20\u4e09 "," Sex ":" 1 "," birthyear ": "1989"}]

Why not Zhang San?


Reply to discussion (solution)

You Json_encode, Chinese?? Not union??

Print (Json_encode ($output));
Change into
Print ($output);

If you want to see Chinese after json_encode, can you? Before you test me? Article: http://blog.csdn.net/fdipzone/article/details/28766357

Json_encode only accepts UTF-8 encoded data.
Json_encode converts multibyte utf-8 characters into two-byte Unicode encoded entity forms when JSON encoding is done
This bouquet in any environment will not cause data distortion due to coding
JSON data format is mainly used for communication with JS, and the browser will convert any encoded data into its working character set Unicode
So, Json_encode's transcoding work helps ease the pressure on the browser.

$con = mysql_connect ("localhost", "root", "1234");
mysql_select_db ("Test", $con);

mysql_query ("Set names UTF8");
mysql_query ("Set Character_set_client=utf8");
mysql_query ("Set Character_set_results=utf8");


$q =mysql_query ("SELECT * from people");
while ($e =mysql_fetch_assoc ($q)) {
$output []= $e; }
Print (Json_encode ($output));
Mysql_close ();
?>



Output Result:
[{"id": "Ten", "Name": "John", "Sex": "1", "Birthyear": "1979"},{"id": "One", "Name:" \u5f20\u4e09 "," Sex ":" 1 "," birthyear ": "1989"}]

Why not Zhang San?





Why did I reload the Wamp, and then become garbled, instead of backwards? Is there a problem with the above code? Or is there a file to configure?

Where do you see garbled characters?

Where do you see garbled characters?


Inexplicably no garbled. It's a decoding problem.

What's the problem with decoding?

What's the problem with decoding?


Thank you moderator, just solved the problem of decoding, in order to make PHP in the browser output time, can display Chinese, I added in front of the echo " "; The original sentence was also received by Android. Is everything printed through PHP sent to the Android client?

Yes, anything PHP prints is sent to the client

  • 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.