PHP and MySQL development page is changing and solved

xiaoxiao2021-04-09  428

In general, there are two reasons for garbled, first because of the CHARSET setting error, resulting in the browser to resolve, and there is a full-screen "Tianshu", followed by the file being wrong. Encoding opens, then saved, such as a text file is originally GB2312 encoded, but re-saved in UTF-8 encoding. To solve the above garbled problem, you first need to know which links in the development involve encoding:

1. File code: Refers to the page file (.html, .php, etc.) itself is coded to be saved. Notepad and Dreamweaver are automatically identifying file encoding when opening the page. And ZendStudio does not automatically identify the encoding. It will only open the file according to the preferences of the configuration, if you don't pay attention, use the error code to open the file, make a modification, saved, garbled ( I have a deep understanding).

2, page declaration code: In the HTML code head, you can use to tell the browser web page what code is used, currently The XXX in the development of the Chinese website is mainly used in GB2312 and UTF-8 coding.

3, database connection encoding: Refers to which coding and database transmission data is performed when the database operation is performed. It should be noted that not to be confused with the database itself, such as the internal default in MySQL is Latin1 encoding, that is, mysql is latin1 Coding to store data, data transferred to mysql by other encoding will be converted to latin1 encoding.

I know which places in the Web development involve encoding. I know the reason for garbled: The above 3 coding settings are inconsistent, because most codents are compatible with ASCII, so the English symbol will not appear, Chinese is unlucky. . Here are some common error and resolution:

1. The database uses UTF8 encoding, and the page declares that the code is GB2312, which is the most common cause of garbled. At this time, it is garbled when you direct SELECT data in the PHP script, you need to use before the query:

MySQL_QUERY ("SET NAMES GBK"); set the mysql connection code, and ensure that the page declares that the encoding is consistent with the connection coding set here (GBK is the expansion of GB2312). If the page is UTF-8 encoding, you can use:

MySQL_Query ("set names utf8"); note that UTF8 is not generally UTF-8. If the code declared by the page is consistent with the internal encoding of the database, it may not set the connection encoding.

Note: In fact, the data input output of MySQL is more complex than the above, and the mysql configuration file is defined in the mysql configuration file My.ini, which is defined in [default-character-set and [mysqld] in [mysqld], DEFAULT. -Character-set To set the encoding used by the client connection and the inside of the database by default. The code specified above is actually the command line parameter character_set_client when the mysql client is connected to the server, tells the client data accepted by the mysql server, not the default encoding.

2, the page declaration code is inconsistent with the file itself, this situation rarely occurs, because if the coding is inconsistent, it is garbled when you do the page. More time is published in the release, modify some small bugs, open the page with error coding and save it. Or use some FTP software to modify files directly online, such as CuteFTP, due to software encoding configuration errors, the conversion is miscode encoded. 3, some friends who rent virtual hosts, clearly the above three codes are set correctly or garbled. For example, the web page is GB2312 encoded. The browser such as IE is always identified as a UTF-8. The web page has been declated that GB2312 is made, manually modifying the browser code to the GB2312, the page is normal. The cause is that the server Apache sets the default encoding of the server globally, adds AddDefaultCharset UTF-8 in httpd.conf. At this time, the server will send the HTTP head to the browser first, and its priority is high than the page, the natural browser is wrong. There are 2 solutions, please include an AddDefaultCharset GB2312 in your own virtual machine to overwrite global configuration, or configure it in your own directory. Htaccess.

转载请注明原文地址:https://www.9cbs.com/read-132949.html

New Post(0)