This article mainly summarizes and introduces several methods to solve the Chinese garbled problem of PHP MYSQL. It is very practical. Friends in need can refer to it. .
Multi-language support has been introduced since MySQL 4.1, but Chinese characters inserted using PHP will appear garbled. No matter what encoding is used, it will not work.
Solving this problem is actually very simple.
1. When creating the table, set the encoding type to gb2312_chinese_ci.
2. Add a line mysql_query("SET NAMES 'gb2312'",$link); to the database connection statement on the PHP page; for example
?
2 3 4 5 6 7
|
$db_host="localhost"; $db_user="root"; $db_password="password"; $db_name="test"; $link=mysql_connect($db_host,$db_user,$db_password); mysql_query("SET NAMES 'gb2312'",$link); $db=mysql_select_db($db_name,$link); $query="select * from user"; $result=mysql_query($query); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | mysql> show variables like 'character_set_%'; -------------------------- -------------------------- ------- | Variable_name | Value | -------------------------- -------------------------- ------- | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | -------------------------- -------------------------- ------- 7 rows in set (0.00 sec) mysql> show variables like 'collation_%'; ----------------------- ------------------ | Variable_name | Value | ----------------------- ------------------ | collation_connection | latin1_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | ----------------------- ------------------ 3 rows in set (0.00 sec) |
The values listed above are the system default values. (It’s strange why the system defaults to latin1 Swedish sorting method)...
When we access the MySQL database through PHP in the original way, even if we set the default character set of the table to utf8 and send the query through UTF-8 encoding, you will find that the data stored in the database is still garbled. The problem lies in this connection layer. The solution is to execute the following sentence before sending the query:
set names 'utf8';
It is equivalent to the following four instructions:
The code is as follows:
Set character_set_client = utf8;
Set character_set_results = utf8;
set character_set_connection = utf8;
set collation_connection = utf8_general_ci
Since the query submitted by the default web page is gb2312 (which can be seen in the form page meta), and mysql treats it as utf8 by default (you can check the character_set_client=utf8 at this time), it must be garbled. In the same way, the results returned by mysql have been converted into character_set_results encoding (it has nothing to do with the encoding of the table). The default is utf8, and the web page treats it as gb2312, so there must be garbled fields such as titles and other fields read from the database. The text in other PHP parts is not garbled.
Solution (by Yijian Piaoxue):
When installing mysql5.0, select the utf8 character set (when using phpmyadmin to create databases and fields, you do not need to select the utf8 character set in the arrangement), and send it after php establishes the connection
Copy the code. The code is as follows:
$link = mysql_connect('localhost', 'root', 'root');
mysql_query("SET NAMES 'utf8'",$link);
At this time, what we see on the web page is still garbled, but it is no longer ????. When we check the source file of the web page, it is completely normal. Open the php source file with notepad, don't save it as utf8 encoding, refresh the web page, and everything is done.
Or, of course, you still need to install utf8 when installing, send set names 'gb2312' in php, and save the php file as notepad's default ansi, which can also display Chinese correctly.
But you can’t send SET NAMES 'utf8' every time you connect. I haven’t found a way to completely solve it.
When installing mysql in this way, the default character set is selected as utf8, which brings another problem. After we enter the mysql console in command.exe, the query results become garbled. We can enter
before querying.The code is as follows:
mysql>set names 'gbk';
or
Copy the code. The code is as follows:
mysql>set names 'gb2312';
It is equivalent to telling the mysql client to use the gb2312 character set, and the result will be correct. gb2312 is a subset of GBK.