Home > Backend Development > PHP Tutorial > Solution to garbled question marks in PHP+MYSQL_PHP Tutorial

Solution to garbled question marks in PHP+MYSQL_PHP Tutorial

WBOY
Release: 2016-07-13 10:46:46
Original
964 people have browsed it

Most of the php and mysql passwords have nothing to do with php. We only need to process the page and mysql encoding to solve the garbled problem. The garbled characters are only garbled in Chinese.

I encountered the problem of garbled characters when using PHP+MYSQL. The solution:

Add SET NAMES UTF8 after mysql_connect to eliminate garbled characters in UTF8 database. For GBK database, use SET NAMES GBK. The code is as follows:

The code is as follows
 代码如下 复制代码

1 $mysql_mylink = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
2 mysql_query("SET NAMES 'GBK'");

Copy code

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:

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.
http://www.bkjia.com/PHPjc/632910.html
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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template