PHP + MYSQL Chinese garbled problem _ PHP Tutorial

Source: Internet
Author: User
PHP + MYSQL Chinese garbled problem. PHP + MYSQL Chinese garbled problem this article mainly summarizes several methods to solve PHP + MYSQL Chinese garbled problem, which is very practical. if you need it, you can refer to it. Chinese garbled characters from MySQL4 PHP + MYSQL

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:

?

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 );

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

?

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 '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 ';

Or

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.

This article mainly summarizes several methods to solve the Chinese garbled characters in PHP + MYSQL. it is very practical. if you need it, you can refer to it. From MySQL 4...

Related Article

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.