echo Display Chinese garbled problem

Source: Internet
Author: User
Tags echo display
The PHP code is as follows:
echo "
  • ISBN: ". $isbn."
    Title: ". $name."
    Author: ". $author."
    ";

  • The data is obtained from the database and then assigned to $ISBN.
    Garbled display

    Add code:
    echo "
     
       ";

    After:


    How to solve Ah??


    Reply to discussion (solution)

    Add: mysql_query ("Set names UTF8") before fetching data;

    Add: mysql_query ("Set names UTF8") before fetching data;


    Still the same.

    What do you see in the phpMyAdmin?
    What code does your database use?

    What do you see in the phpMyAdmin?
    What code does your database use?


    Utf8_general_ci

    Add: mysql_query ("Set names UTF8") before fetching data;



    This is the code:
    The above Connection database Mysql_set_charset ("GBK"); $result = mysql_query ("select * from book_list LIMIT 0, 30"); Query database while ($bookInfo = Mysql_fetch_array ($result)) {//Return query result to array   $ISBN = $bookInfo ["B_ISBN"];//Remove data from array    name = $bookInfo ["B_name"];    $author = $bookInfo ["B_author"];echo "
  • ISBN: ". $isbn."
    Title: ". $name."
    Author: ". $author."
  • "; Output Data}


    Add: mysql_query ("Set names UTF8") before fetching data;



    This is the code:
    The above Connection database Mysql_set_charset ("GBK"); $result = mysql_query ("select * from book_list LIMIT 0, 30"); Query database while ($bookInfo = Mysql_fetch_array ($result)) {//Return query result to array   $ISBN = $bookInfo ["B_ISBN"];//Remove data from array    name = $bookInfo ["B_name"];    $author = $bookInfo ["B_author"];echo "
  • ISBN: ". $isbn."
    Title: ". $name."
    Author: ". $author."
  • "; Output Data}



    Comment out
    Mysql_set_charset ("GBK");
    In front of ECHO, add:
    echo "
     
       ";

    Just fine.
    Also, if you add mysql_query ("Set names UTF8"), all Chinese is garbled.

    When displayed directly, the contents of the database read are normal. But the title, the author garbled.
    Here you can know that your PHP file is UTF8, so add echo " ";
    The title, the author, is normal.

    Then the original read the contents of the database into garbled. It is certain that the contents of the database are not in UTF8 format. You can use Iconv conversions once to see the problem.

    This sentence is changed to this echo "
  • ISBN: ". $isbn."
    Title: ". Iconv (' GBK ', ' UTF-8 ', $name)."
    Author: ". Iconv (' GBK ', ' UTF-8 ', $author)."
  • "; Output data

    Why do you want to add Mysql_set_charset ("GBK");?

    No wonder this is the case, so I think the database is not UTF8.

    Mysql_set_charset ("GBK"); equals mysql_query ("Set names GBK"); You've turned around once and then UTF8 ...

    UTF-8 encoded titles in the GBK environment will be displayed as CEN??
    The data read from the database now displays normal
    This is: The data read from the database is GBK encoded, this can be issued from the Utf-8 language declaration, garbled situation in turn to substantiate

    According to your current situation, you need to $ISBN = Iconv (' GBK ', ' utf-8 ', $ISBN); Code conversion from GBK to Utf-8 for the data that will be read
    And should have the header (' Content-type:text/html;charset=utf-8 '); The head of the language declaration

    This behavior occurs because:
    The database is Latin1
    field is UTF8.
    Insert yes the data is GBK
    Because the character set is not declared when inserting data, the contents of GBK are inserted into the field as Latin1
    The read-out also does not declare the character set, so the data in the field is read back as Latin1.
    In fact, this is normal, the database should not change the stored data
    Otherwise put in the warehouse is a car, get back into a motorcycle, that is not messy set it?

    This is true for data already in the database, and transcoding is required if you do not want to give up (very troublesome)
    When inserting and reading data in the future, it is important to note that the Set Names XXX directive is executed first, note: This xxx is the character set name of the current data
    For example, the GBK data is inserted into the mysql_query ("Set names GBK");
    mysql_query ("Set names UTF8") to retrieve the data according to Utf-8;

    UTF-8 encoded titles in the GBK environment will be displayed as CEN??
    The data read from the database now displays normal
    This is: The data read from the database is GBK encoded, this can be issued from the Utf-8 language declaration, garbled situation in turn to substantiate

    According to your current situation, you need to $ISBN = Iconv (' GBK ', ' utf-8 ', $ISBN); Code conversion from GBK to Utf-8 for the data that will be read
    And should have the header (' Content-type:text/html;charset=utf-8 '); The head of the language declaration

    This behavior occurs because:
    The database is Latin1
    field is UTF8.
    Insert yes the data is GBK
    Because the character set is not declared when inserting data, the contents of GBK are inserted into the field as Latin1
    The read-out also does not declare the character set, so the data in the field is read back as Latin1.
    In fact, this is normal, the database should not change the stored data
    Otherwise put in the warehouse is a car, get back into a motorcycle, that is not messy set it?

    This is true for data already in the database, and transcoding is required if you do not want to give up (very troublesome)
    When inserting and reading data in the future, it is important to note that the Set Names XXX directive is executed first, note: This xxx is the character set name of the current data
    For example, the GBK data is inserted into the mysql_query ("Set names GBK");
    mysql_query ("Set names UTF8") to retrieve the data according to Utf-8;



    Praise! Reasonable Very good analysis.

    Why do you want to add Mysql_set_charset ("GBK");?

    No wonder this is the case, so I think the database is not UTF8.

    Mysql_set_charset ("GBK"); equals mysql_query ("Set names GBK"); You've turned around once and then UTF8 ...



    First contact with PHP, need to connect to the database, and then Niang help find the code.
  • Related Article

    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.