A solution for garbled pages during PHP and MySQL Development

Source: Internet
Author: User

In general, there are two possible causes for Garbled text. The first reason is the incorrect encoding (charset) setting, which leads to the incorrect encoding resolution by the browser, resulting in the "tianshu", which is full of screens ", secondly, the file is opened with an error code, and then saved, for example, a text file originally GB2312 encoding, but opened with UTF-8 encoding and then saved. To solve the above garbled code problem, you must first know which stages of development involve encoding:

1. file encoding: indicates the encoding of the page file (.html,. php, etc. Notepad and Dreamweaver automatically recognize the file encoding when opening the page, so there is no problem. ZendStudio does not automatically recognize the encoding. It only opens the file with a certain encoding according to the preference configuration. If you do not pay attention to it during work, use the error code to open the file, after the modification, the garbled code will appear as soon as it is saved (I have a deep understanding ).

2. Page declarative encoding: in the HTML code HEAD, you can use <meta http-equiv = "Content-Type" content = "text/html; charset = "XXX"/> to tell the browser web page using what encoding, currently Chinese website development XXX mainly uses GB2312 and UTF-8 two types of encoding.

3. Database Connection encoding: it refers to the encoding used to transmit data with the database during database operations. Note that it should not be confused with the database encoding, for example, MySQL uses latin1 encoding by default. That is to say, Mysql uses latin1 encoding to store data. Data transmitted to Mysql using other encoding will be converted to latin1 encoding.

When we know where encoding is involved in WEB development, we also know the cause of garbled code: the three encoding settings are inconsistent, because most of the encodings are compatible with ASCII, so the English symbols will not appear, and Chinese characters will be unlucky. The following are some common errors and solutions:

1. The database uses UTF8 encoding, while the page declarative encoding is GB2312, which is the most common cause of garbled code. In this case, the SELECT data in the PHP script is garbled and needs to be used before query:

Mysql_query ("set names gbk"); To SET the MYSQL connection encoding, ensure that the page declarative encoding is consistent with the connection encoding 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 UTF8 is not a commonly used UTF-8. If the encoding stated on the page is consistent with the internal encoding of the database, no connection encoding can be set.
Note: In fact, MYSQL data input and output are more complex than described above. MYSQL configuration file my. ini defines two default encodings, they are default-character-set in [client] and default-character-set in [mysqld] to set the encoding used for client connection and database respectively by default. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server to tell the MYSQL server what encoding the client data is received, rather than the default encoding.

2. The page declarative encoding is inconsistent with the file encoding. this rarely happens, because if the encoding is inconsistent, the attacker will see garbled code in the browser when making the page. More often, it is caused by modifying some minor bugs after the release, opening the page with error code, and saving it. Or you can use some FTP software to directly modify files online, such as CuteFTP. The conversion error is caused by incorrect software encoding.

3. Some friends who rent a VM clearly confirm that the above three codes are correctly set and there are still garbled characters. For example, the web page is GB2312 encoding, IE and other browsers open but always recognized as a UTF-8, the web page HEAD has been declared is GB2312, manually modify the browser code to GB2312 after the page shows normal. The cause is that the server Apache sets the server's global default encoding, added the AddDefaultCharset UTF-8 in httpd. conf. At this time, the server will first send an HTTP header to the browser, which has a higher priority than the declarative encoding in the page, and the natural browser will recognize the error. There are two solutions. The administrator needs to add adddefacharcharset GB2312 to the virtual machine in the configuration file to overwrite the global configuration, or configure it in the. htaccess directory.

Note: In the header area of the HTML page, <meta http-equiv = "Content-Type" content = "text/html; charset = "XXX"/> must be written before <title> XXX </title>. Otherwise, the page will be blank (IE + PHP only ).

The above is my summary and experience with problems I encountered in my previous work. If you have any mistakes, please correct me!

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.