PHP + MYSQL Chinese garbled problem

Source: Internet
Author: User
This article mainly summarizes several methods to solve Chinese garbled characters in PHP + MYSQL. it is very practical. if you need it, you can refer to it. Multi-language support was introduced from MySQL 4.1, but garbled characters may appear in the Chinese language inserted using PHP.

Solving this problem is actually very simple.

1. set the encoding type to gb2312_chinese_ci when creating the table.

2. add a mysql_query ("set names 'gb2312'", $ link) to the database connection statement on the PHP page. 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);

This line is added to both the write and read pages, so that Chinese characters in MYSQL can be displayed normally.

Related information:

Multi-language support has been introduced since MySQL 4.1, and some features have exceeded those of other database systems.
Character Set Support of MySQL has two aspects: Characterset and Collation ). The support for character sets is refined to four levels: server, database, table, and connection ).
You can run the following two commands to view the character set and sorting method settings of the system :!

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 of the system. (It's strange how latin1's Swedish sorting method is used by default )...
When we access the MySQL database through PHP in the original way, even if the default character set of the table is set to utf8 and the query is sent through the UTF-8 encoding, you will find that the database is still garbled. The problem lies in the connection layer. The solution is to execute the following statement before sending the query:
Set names 'utf8 ';
It is equivalent to the following four commands:

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

By default, the query submitted by the web page is gb2312 (which can be seen in the form page meta), and mysql treats it as utf8 by default (character_set_client = utf8 can be found at this time), so garbled characters are inevitable. Similarly, the results returned by mysql are converted to character_set_results encoding (not related to the table encoding). The default value is utf8, and the webpage treats it as gb2312, therefore, it is inevitable that fields such as titles read by the database are garbled while other php text is not garbled.

Solution ):
Select the utf8 character set when installing mysql5.0 (you do not need to select the utf8 character set when creating a database and a field using phpmyadmin), and send it after establishing a connection with php.

The code is as follows:

$ Link = mysql_connect ('localhost', 'root', 'root ');
Mysql_query ("set names 'utf8'", $ link );

What we see on the webpage is still garbled, but it is no longer ???? View the webpage source file. it is completely normal. Open the php source file in Notepad, do not save it as utf8 encoding, and then refresh the page, all done.
Or, of course, you still need to install utf8 when installing it. in php, send set names 'gb2312', and save the php file as the default ansi of notepad, which can also display Chinese characters correctly.

However, the set names 'utf8' cannot be sent every time a connection is established. no solution is found yet.

When mysqlis installed in this example, the sequence generator set is utf8followed by a question. after we enter the mysql console in command.exe, the query result becomes garbled. we can enter

The code is as follows:

Mysql> set names 'gbk ';


The code is as follows:

Mysql> set names 'gb2312 ';

It is equivalent to telling the mysql client to use the gb2312 character set. The result is correct, and gb2312 is a subset of GBK.

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: 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.