Chinese garbled Chinese tutorial and mysql tutorial Chinese garbled solution
<html>
<head>
<title> </ title>
</ head>
<body>
<? php
$ 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 ("
<br> ");
while ($ row = mysql_fetch_object ($ rs)) {
print ($ row-> name. ":". $ row-> age. "<br>");
}
mysql_close ($ conn);
?>
Display as follows:
resource id # 1
dd: 54
ddd: 8
??: 15
???: 25
??: 32
Mysql encoding: utf8, gbk have tried. mysql font and command line display are correct.
Questions added:
Garbled
???: 15
???: 25
??: 32
These few lines, the value of the database tutorial is Chinese characters. The question mark is displayed.
Solution:
In $ rs = mysql_db_query ("lib", "select * from mytb", $ conn);
Plus in front
mysql_query ("set names gb2312"); or mysql_query ("set names gbk");
Now look at the solution to the problem of garbled
.gb2312, gbk, utf8 and other support multi-byte encoding character sets can store Chinese characters, the number of characters in gb2312 much less than gbk, and gb2312, gbk, etc. can be encoded under utf8.
Use the command show variables like 'character_set_%'; 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)
Display Chinese garbled There are two main settings: character_set_connection and character_set_results,
If your two settings do not support Chinese encoding, there will be garbled, as long as: set character_set_results = gbk; Set the Chinese encoding.
Three .set names charset_name; You can set all the client's character set.