PHP and MySQL in the development of the page garbled a solution _php skills

Source: Internet
Author: User
Tags html page mysql client mysql in php and php and mysql php script blank page
In general, the occurrence of garbled characters have 2 kinds of reasons, the first is because the encoding (charset) set error, causing the browser to parse with the wrong encoding, resulting in a full screen messy "heavenly book", followed by the file is the wrong code to open, and then save, such as a text file was originally GB2312 encoded, It is opened and saved with UTF-8 encoding. To solve the above garbled problem, first need to know which links in the development of the Code:

1, file encoding: Refers to the paging file (. html,.php, etc.) itself is the type of code to save. Notepad and Dreamweaver automatically recognize the file encoding when they open the page, so there is no problem. But Zendstudio will not automatically recognize the code, it will only be fixed according to the preferences of the configuration of a code to open the file, if the work is not noticed, with the wrong code to open the file, made a change after a save, garbled appeared (I have a deep experience).

2, page statement code: In the HTML code head inside, you can use <meta http-equiv= "Content-type" content= "text/html;" charset= "xxx"/> To tell the browser what encoding the Web page, the current Chinese web site in the development of XXX is mainly used in the GB2312 and UTF-8 two kinds of coding.

3, database Connection code: Refers to the database operation when the encoding and database transmission data, here need to pay attention to the database itself is not confused with the coding, such as MySQL internal default is latin1 encoding, that is, MySQL is latin1 encoded to store data, Data transmitted to MySQL in other encodings is converted to latin1 encoding.

Know where the web development involved in coding, also know the reason for the garbled code: the above 3 coding settings are inconsistent, because most of the encoding is compatible with ASCII, so the English symbol will not appear, Chinese on the bad luck. Here are some common error situations and resolutions:

1, the database uses UTF8 code, and the page affirms the code is GB2312, this is the most common generation of garbled reason. At this time in the PHP script directly select data out is garbled, need to use 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"); Attention is UTF8 rather than 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's data input and output is more complex than the above, the MySQL configuration file My.ini defines 2 default encodings, respectively, in [client] Default-character-set and [mysqld] Default-character-set to set the default client connection and the code used inside the database. The code we specify above is actually a MySQL Client Connection server command line parameter character_set_client, to tell the MySQL server received the client data is what encoding, rather than the default encoding.

2, page declaration code and the file itself is inconsistent with the code, this situation rarely occurs, because if the code inconsistent with the page when the browser to see is garbled. More often it is after the release to modify some small bugs, to open the page in error encoding and then save the resulting. Or with some FTP software directly online modify files, such as CuteFTP, due to software coding configuration errors caused by the conversion of the wrong encoding.

3, some rented virtual host friends, obviously the above 3 codes are set up correctly or garbled. For example, the Web page is GB2312 code, ie, such as browser Open but always recognized as UTF-8, the page head has been declared 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 code, in Httpd.conf added Adddefaultcharset UTF-8. At this time the server will first send HTTP headers to the browser, its priority than the page in the Declaration of High Code, the natural browser to identify the wrong. There are 2 solutions, ask the administrator to add a adddefaultcharset GB2312 to the configuration file's own virtual machine to override the global configuration, or configure it in the. htaccess of your own directory.

2006.4.4 Note: Thank Junchen inform: In the HTML page Head area, <meta http-equiv= "Content-type content=" text/html; charset= "XXX"/> This sentence must be written in front of <title>XXX</title>, otherwise it will result in a blank page (only ie+php).

Above is my previous time work to encounter the question summary and the experience, if has the mistake, welcome corrected!

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.