PHP + MySQL garbled Solution

Source: Internet
Author: User
Tags mysql commands

All of them have solved the issue of garbled code in JSP, but some people still haven't solved the problem of MySQL garbled code, including myself, so I found some information, we hope this time we can completely solve the problem of garbled characters in the MySQL database.
The first method is an incisive summary:
The VM is often changed, and the MySQL versions of different service providers are different. After data is imported, garbled characters and other problems cannot be displayed normally. I checked a lot of information and summarized my skills:

This is the case when importing data in windows.
Use MySQL commands
Run the doscommand to enter the MySQL bin directory and enter the mysql-uroot-P Password Database Name <the database to be restored. For example, we need to name disk D as test. the SQL database is restored to the local database Test2, so MySQL is used for foreign hosts before mysql-uroot-P password test2. X series, I feel better, no matter GBK and UTF-8 are not garbled, did not expect the new host MySQL is 5.0 version, after importing data, read out with PHP is question mark, garbled text. I remember that I had a previous experience with Garbled text during Switching. The reason must be the difference between MySQL versions.

I had to check the information and found a solution. I added a set names utf8 sentence after mysql_connect to eliminate garbled characters in the utf8 database. For GBK databases, I used set names GBK,CodeAs follows:

$ Mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass );
Mysql_query ("set names 'gbk '");

The database character set is UTF-8.

The connection statement uses this
Mysql_query ("set names 'utf8 '");
Mysql_query ("set Character Set utf8 ");
Mysql_query ("set character_set_results = utf8 '");

Another way is to change the MySQL character set if your machine is running. It is generally valid in MySQL 4 and MySQL 5.

Method 2: A good explanation. Try it.

Summary of MySQL garbled processing:

(1) Chinese characters are processed normally in Java. Displaying garbled characters in the CMD client is a problem with character sets.

(2) The length of the field is long enough. However, when a Chinese character is inserted, the system prompts com. MySQL. JDBC. mysqldatatruncation: Data truncation: data too long for column.

(3) garbled characters are the final issue of character sets. We should consider the following aspects from Character Set settings: Server, client, database, connection, and results.

------------------------- Solution ----------------------

(1) modify the my. ini (MySQL server instance configuration file)

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.