Php-mysql Chinese garbled problem.

Source: Internet
Author: User
Tags mysql client

Since MySQL 4.1 introduced multi-language support, but the Chinese inserted in PHP will appear garbled. No matter what code you use.

It is very simple to solve the problem.

1. Set the encoding type to GB2312_CHINESE_CI when building the table.

2. The database connection statement on the PHP page adds a line mysql_query ("SET NAMES ' gb2312 '", $link); For example

$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 show up normally.

Related information:

Multi-lingual support has been introduced from MySQL 4.1, and some features have surpassed other database systems.
MySQL4.1 's character set support (Character set supports) has two facets: Character set (Characterset) and sort (Collation). Support for character sets is refined to four levels: server, database, data table (table), and connection (connection).
To view the settings of the system's character set and sort by the following two commands:!
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 default values for the system. (It's strange how the system defaults to the Swedish sort of latin1) ...
When we access the MySQL database by PHP in the original way, even if the default character set of the table is UTF8 and the query is sent through 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:
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 submits the query is gb2312 (can be seen in the form page meta), and MySQL defaults to it as a utf8 (can be found at this time Character_set_client=utf8), so it must be garbled. Similarly, the result of MySQL return is converted to CHARACTER_SET_RESULTS encoded (regardless of the encoding of the table), the same default is UTF8, and the page page gb2312 it when processing, So there must be a title, such as the database read out of the field is garbled and other parts of PHP text is not garbled phenomenon.

Solution (by a sword of Snow):
Select the UTF8 character set when installing mysql5.0 (you do not need to collate the UTF8 character set when you create a database and field with phpMyAdmin) and send it after PHP has established a connection
$link = mysql_connect (' localhost ', ' root ', ' root ');
mysql_query ("SET NAMES ' UTF8 '", $link);
At this time we see in the Web page is garbled but is not???? , viewing the Web page source file is completely normal. Use Notepad to open the PHP source file, do not save as UTF8 encoding, and then refresh the page, all done.
Or, of course, to install still want to UTF8 installation, in PHP send set names ' gb2312 ', while PHP file saved as Notepad default ANSI, can also display Chinese correctly.

However, it is not always possible to send the set NAMES ' UTF8 ' every time the connection is complete, how to solve the problem has not found the method.

In this way, the default character set selected as UTF8 after MySQL installation is a problem, after we entered the MySQL console in Command.exe, the query results are garbled, we can enter before the query
Mysql>set names ' GBK ';
Or
Mysql>set names ' gb2312 ';
Equivalent to telling the MySQL client to use the gb2312 character set, the result is correct, and gb2312 is a subset of GBK.

Php-mysql Chinese garbled problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.