PHP set Chinese cookie, read echo out garbled, write into MySQL and no garbled, why?

Source: Internet
Author: User
The header is already set to Utf-8 on all pages. Garbled results in no way and data in the row comparison, and the original text is not equal.

Reply content:

The header is already set to Utf-8 on all pages. Garbled results in no way and data in the row comparison, and the original text is not equal.

Find the reason, because the write database is processed with the function mysql_real_escape (), and the removal does not reverse processing results in Chinese garbled, using stripslashes () processing after reading normal.

First you should look at the file encoding, if the file encoding is Gbk,header also wood, the header just tells the client the following content in accordance with UTF8 parsing, and the file or the content itself encoding regardless, as to the database will not garbled, this is related to the character set range, Database will be garbled can give an example: if the file is GBK encoding, database field is UTF8, stored when set names UTF8, read out in UTF8 file display when set CharSet is UTF8, my understanding is not garbled, Because the GBK character set range is smaller than UTF8, GBK content to UTF8 encoding can be stored under, and UTF8 storage and UTF8 display is not garbled, if the GBK read out I think it will be garbled, although the file is GBK, but it is stored in the database UTF8, And UTF8 is accounted for three bytes or more, and GBK is two bytes, so GBK can not show UTF8 content, so will garbled, on paper, no practice, in this fallacy, I just said I am so understanding!

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