Php+mysql the solution to the problem of garbled question mark

Source: Internet
Author: User
Tags character set mysql client php script

Using Php+mysql encountered a character disorder problem, the solution:

After mysql_connect add a set NAMES UTF8, you can make the UTF8 database to eliminate garbled, for the GBK database uses set NAMES GBK, 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 '");

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 '");


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.

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.