PHP and MySQL in the development of page garbled generation and resolution _php tutorial

Source: Internet
Author: User
Tags mysql client php and mysql dreamweaver
1, file encoding: Refers to the page file (. html,.php, etc.) itself is what encoding to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page and are therefore less problematic. And Zendstudio does not automatically identify the code, it will only be fixed in accordance with the preferences of the configuration of a certain encoding to open the file, if the working time a note, with the wrong code to open the file, made a change after the save, garbled appear (I have experience).

2, page declaration code: In the HTML code head inside, you can use To tell the browser page adopted what code, the current Chinese website development in XXX is mainly used in GB2312 and UTF-8 two kinds of coding.

3, database Connection code: Refers to the database operation in which encoding and database transfer data, it is important to note that the database itself is not confused with the code, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoding to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding.

Know the Web development where the coding, also know the reason for garbled: The above 3 encoding settings are inconsistent, because the majority of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese is unlucky. Here are some common error conditions to resolve:

1, the database using UTF8 encoding, and page declaration code is GB2312, which is the most common cause of garbled. At this time in the PHP script directly inside the Select data is garbled, need to be used before the query:

mysql_query ("SET NAMES GBK");

To set the MySQL connection code to ensure that the page declaration code is consistent with the connection code set here (GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use:

mysql_query ("SET NAMES UTF8");

Note that it is a UTF8 rather than a general UTF-8. If the code of the page declaration is consistent with the internal encoding of the database, the connection code can be set.
Note: In fact, MySQL data input and output is more complicated than the above, the MySQL configuration file My.ini defined 2 default encoding, respectively [client] in the Default-character-set and [mysqld] Default-character-set to set the default client connection and the encoding used internally by the database. The code we specified above is actually the command line parameter character_set_client of the MySQL client to connect to the server, to tell the MySQL server what encoding the client data is accepted, not the default encoding.

2, the page declaration code and the file itself encoding inconsistent, this situation rarely occurs, because if the code is inconsistent with the page when the browser sees is garbled. More often than not after the release of some minor bugs, the wrong code to open the page and save the resulting. or using some FTP software to directly modify the file online, such as CuteFTP, due to the software encoding configuration error, resulting in the wrong conversion encoding.

3, some rented virtual host friends, clearly the above 3 codes are set correctly or garbled. For example, the Web page is GB2312 encoded, IE and other browser open but always recognized as UTF-8, the page head has been declared to be GB2312, manually modify the browser code for GB2312 after the page display normal. The reason is that the server Apache set the server global default encoding, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, the priority is higher than the page stated that the code is high, the natural browser is identified wrong. There are 2 solutions, please add a adddefaultcharset GB2312 to the config file's own virtual machine to cover the global configuration, or configure it in the. htaccess of your own directory.

http://www.bkjia.com/PHPjc/318772.html www.bkjia.com true http://www.bkjia.com/PHPjc/318772.html techarticle 1, file encoding: Refers to the page file (. html,.php, etc.) itself is what encoding to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page and are therefore less likely to appear ...

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