1. File encoding: refers to the encoding in which the page file (.html, .php, etc.) itself is saved. Notepad and Dreamweaver will automatically recognize the file encoding when opening the page, so there will be less problems. However, ZendStudio does not automatically recognize the encoding. It will only open the file in a certain encoding according to the configuration of the preferences. If you accidentally open the file with the wrong encoding while working, and after making the modification, garbled characters will appear ( I feel it deeply).
2. Page declaration encoding: In the HTML code HEAD, you can use to tell the browser what encoding the web page uses. Currently In Chinese website development, XXX mainly uses GB2312 and UTF-8 encoding.
3. Database connection encoding: refers to which encoding is used to transmit data to the database when performing database operations. It should be noted here that it should not be confused with the encoding of the database itself. For example, the default internal encoding of MySQL is latin1 encoding, which means that Mysql is based on Latin1 encoding is used to store data. Data transmitted to Mysql in other encodings will be converted into latin1 encoding.
Now that we know where encoding is involved in WEB development, we also know the reasons for garbled characters: the above three encoding settings are inconsistent. Since most of the various encodings are compatible with ASCII, English symbols will not appear, and Chinese characters will be unlucky. . The following are some common error situations and solutions:
1. The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common cause of garbled characters. At this time, the directly Select data in the PHP script will be garbled. You need to use it before querying:
mysql_query("SET NAMES GBK");
To set the MYSQL connection encoding, ensure that the page declaration encoding is the same as the connection encoding set here Consistent (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 UTF8 instead of the commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding.
Note: In fact, the data input and output of MYSQL is more complicated than what is mentioned above. There are 2 default encodings defined in the MYSQL configuration file my.ini, which are the default -character-set in [client] and the one in [mysqld]. default-character-set sets the encoding used by default for client connections and database internals. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server, which tells the MYSQL server what encoding the client data received is, instead of using the default encoding.
2. The page declaration encoding is inconsistent with the encoding of the file itself. This rarely happens because if the encoding is inconsistent, what the artist sees in the browser when creating the page will be garbled characters. More often than not, it is caused by fixing some minor bugs after release, opening the page in the wrong encoding and then saving it. Or you use some FTP software to directly modify files online, such as CuteFTP. Due to incorrect software encoding configuration, the wrong encoding is converted.
3. Some friends who rent virtual hosts still have garbled codes even though the above three encodings are set correctly. For example, if the web page is encoded in GB2312, it is always recognized as UTF-8 when opened by browsers such as IE. The HEAD of the web page has already stated that it is GB2312. After manually changing the browser encoding to GB2312, the page displays normally. The reason is that the server Apache sets the global default encoding of the server and adds AddDefaultCharset UTF-8 in httpd.conf. At this time, the server will first send the HTTP header to the browser, and its priority is higher than the encoding declared in the page. Naturally, the browser will recognize it incorrectly. There are two solutions. Administrators should add AddDefaultCharset GB2312 to the configuration file of their own virtual machine to override the global configuration, or configure it in .htaccess in their own directory.
The above has introduced the generation and solution of garbled pages in the development of PHP and MySQL on the official website of marc by marc jacobs, including the content of the official website of marc by marc jacobs. I hope it will be helpful to friends who are interested in PHP tutorials.