This article mainly summarizes several ways to solve php+mysql Chinese garbled problem, very practical, the need for small partners can refer to.
Multi-language support is introduced from MySQL 4.1, but the Chinese in PHP will appear garbled. No matter what code you use.
To solve this problem is very simple indeed.
1. The encoding type is gb2312_chinese_ci when the table is being built.
2. The database connection statement in the PHP page adds a line mysql_query ("SET NAMES ' gb2312 '", $link); For example
?
| 1 2 3 4 5 6 7 8 9 10 |
$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); |
Both the Write page and the Read page are added to this line. So the Chinese in MySQL will be able to display the normal.
Related information:
Multi-language support has been introduced from MySQL 4.1, and some features have surpassed other database systems.
MySQL4.1 's character set support (Character set Support) has two aspects: Character set (Characterset) and sort (collation). The support for character sets is refined to four tiers: Servers (server), databases (database), Data tables (table), and connections (connection).
To view the settings of the system's character set and sort, you can use the following two commands:!
?
| 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 ' Collati on_% '; +----------------------+-------------------+ | 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 default values for the system. (It's strange how the system defaults to the Swedish sort of latin1) ...
When we access the MySQL database through PHP in the way we used to, even if we set the default character set of the table to UTF8 and send the query via UTF-8 encoding, you will find that the database is still garbled. The problem is on the connection connection layer. The workaround is to execute the following sentence before sending the query:
Set names ' UTF8 ';
It corresponds to the following four-sentence instruction:
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
Because the default Web page submitted query is gb2312 (in the form page meta can see), and MySQL defaults to it as a utf8 (can be found at this time Character_set_client=utf8), so necessarily garbled. Similarly, the result of MySQL return has been converted to character_set_results encoding (regardless of the table encoding), the same default is UTF8, and the page page to treat it when gb2312, so there must be a title, etc. by the data Library read out of the field is garbled and other PHP part of the text is not garbled phenomenon.
Resolved (by a sword snowing):
To select the UTF8 character set when you install mysql5.0 (you do not need to select the UTF8 character set when you create a database and a field with phpMyAdmin) and send it after you establish a connection to PHP
Copy code code as follows:
$link = mysql_connect (' localhost ', ' root ', ' root ');
mysql_query ("SET NAMES ' UTF8 '", $link);
At this time we see in the Web page is still garbled but not???? , viewing the Web page source file is completely normal. Use Notepad to open the PHP source file, do not save for UTF8 code, and then refresh the page, all done.
Or, of course, you still have to install the UTF8 installation, in PHP to send set names ' gb2312 ', while the PHP file saved as Notepad default ANSI, also can correctly display Chinese.
But you can't always send a set NAMES ' UTF8 ' every time you connect, and you can't find a way to solve it completely.
This installation of MySQL when the default character set for UTF8 and then brought a problem, we entered the MySQL console in Command.exe, the query result has become garbled, we can input before the query
The code is as follows:
Mysql>set names ' GBK ';
Or
Copy code code as follows:
Mysql>set names ' gb2312 ';
The equivalent of telling the MySQL client to use the gb2312 character set, the result is correct, gb2312 is a subset of GBK.