Home > Article > Backend Development > The problem of garbled Chinese characters in PHP and the solution to the Chinese garbled characters in MySql_PHP Tutorial
The problem of Chinese garbled characters in the PHP tutorial and the solution to the Chinese garbled characters in the MySQL tutorial
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='000000';
$mysql_database='lib';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql="select name,age from mytb";
print($conn);
$rs=mysql_db_query("lib","select * from mytb",$conn);
print("
");
while($row = mysql_fetch_object($rs)){
print ($row->name.":".$row->age."
");
}
mysql_close($conn);
?>
is displayed as follows:
resource id #1
dd:54
ddd:8
??:15
???:25
??:32
MySQL encoding: utf8, gbk have been tried. Both mysql font and command line display are correct.
Question supplement:
Garbled code:
???:15
???:25
??:32
In these lines, the values in the database tutorial are Chinese characters. What is displayed is a question mark.
Solution:
In $rs=mysql_db_query("lib","select * from mytb",$conn);
Preceded by
mysql_query("set names gb2312");or mysql_query("set names gbk");
Let’s take a look at how to solve garbled characters
.gb2312, gbk, utf8 and other character sets that support multi-byte encoding can store Chinese characters. The number of Chinese characters in gb2312 is much less than gbk, and gb2312, gbk, etc. can all be encoded under utf8.
2. Use the command show variables like 'character_set_%'; to view the current character set settings:
mysql> show variables like 'character_set_%';
+--------------------------+--------+
| variable_name | value |
+--------------------------+--------+
| character_set_client | gb2312 |
| character_set_connection | gb2312 |
| character_set_database | gb2312 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
7 rows in set (0.02 sec)
(The character set here is gb2312)
There are two main settings for displaying Chinese garbled characters: character_set_connection and character_set_results,
If your two settings do not support Chinese encoding, garbled characters will appear. Just: set character_set_results =gbk; to set the Chinese encoding.
3.set names charset_name; You can set all character sets of the client at one time.