1 $mysql_mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
2 mysql_query("SET NAMES 'GBK'");
代码如下 |
复制代码 |
1 mysql_query("SET NAMES 'UTF8'");
2 mysql_query("SET CHARACTER SET UTF8");
3 mysql_query("SET CHARACTER_SET_RESULTS=UTF8'"); |
The database character set is utf-8
Use this for connection statements
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'"); |
Here are some common error conditions 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 direct SELECT data in the PHP script will be garbled. You need to use: mysql_query("SET NAMES GBK"); before querying to set the MYSQL connection encoding to ensure that the page declaration 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 rather than 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 default-character-set and [mysqld] in [client]. The default-character-set in is used to set the encoding used by the default client connection and the internal database. 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.
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632910.htmlTechArticleMost of the php and mysql passwords have nothing to do with php. We only need to process the page and mysql encoding. It can solve the problem of garbled characters. The garbled characters are only garbled in Chinese. Use PH...
|