Php and mysql passwords are mostly irrelevant to php. In this case, we only need to process the page and mysql encoding to solve the Garbled text problem. The Garbled text is only garbled Chinese characters.
PHP + MYSQL has encountered character disorder. solution:
Add a set names UTF8 clause after mysql_connect to eliminate garbled characters in the UTF8 database. set names gbk is used for GBK databases. The Code is as follows:
| The Code is as follows: |
Copy code |
|
1 $ mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass );
2 mysql_query ("set names 'gbk '"); |
The database character set is UTF-8.
The connection statement uses this
| The Code is as follows: |
Copy code |
1 mysql_query ("set names 'utf8 '");
2 mysql_query ("set character set UTF8 ");
3 mysql_query ("SET CHARACTER_SET_RESULTS = utf8 '"); |
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. You need to use mysql_query ("set names gbk") before querying 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 it is UTF8 instead of a general 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.