Solve the problem of php access to mysql4.1 garbled characters

Source: Internet
Author: User
: Solve the problem of php access to mysql4.1 garbled code. Read and solve the problem of php access to mysql4.1 garbled code. the multi-language support introduced from MySQL4.1 is indeed great, and some features have exceeded other database systems. The multi-language support introduced since MySQL 4.1 is indeed great, and some features have exceeded other database systems. However, during the test, I found that using a PHP statement before MySQL 4.1 to operate the MySQL database may cause garbled characters, even if the table character set is set.

MySQL 4.1 Character Set Support has two aspects: Character set 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 three commands:

SET character_set_client = utf8;

SET character_set_results = utf8;

SET character_set_connection = utf8;

Try again.

The above content solves the problem of php access to mysql 4.1 garbled characters. For more information, see PHP Chinese network (www.php1.cn )!

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.