How to solve Chinese garbled characters in the MySQL database?

Source: Internet
Author: User

The following articles mainly introduce the actual solution of Chinese garbled characters in the MySQL database, which can be said to be more practical. We all know that when using the MySQL database, we often encounter garbled characters during the actual operation of inserting some Chinese characters into it, and all Chinese characters are used? .

Most of the reasons for this are Character Set mismatch.

In MySQL, if the default character set is used, the latin1 character set is used by default during database creation and table creation, which is ISO 8859-1 Western European character set.

When Chinese characters are inserted, garbled characters are displayed if they do not match.

I use MySQL 5.0 and use C # To operate the MYSQL database. when inserting Chinese characters, I found garbled characters.

It is preliminarily estimated that the character set is not paired. After searching, it is found that MSYQL uses latin1 by default. Therefore, the character setting in the database configuration file is changed to utf8, and the Chinese character is converted into utf8 in C #. After insertion, it is still garbled.

MySQL also supports gb2312, and changes the character setting of MySQL to gb2312, and uses Default in C #. After MySQL is restarted, the operation is still invalid.

Remember that although MySQL's character set is set to gb2312 or utf8, the created databases and Tables still use the default character set, that is, the original western European character set, which is the best, I had to reluctantly Delete the database and create a database and table again with the gb2312 character set.

After you use C # To insert a Chinese character, everything is OK.

After installing the MySQL database, you can manually or use the configuration tool. the default character set in the INI file is changed to gb2312. When you create a new database or table, you do not need to specify the character set in the statement. The default value is gb2312. The Chinese Character Set is displayed normally.

1. A general statement is used to create a database:

 
 
  1. create database dbname; 

2. When creating a table in the database, the statement is:

 
 
  1. create table tbname(..........)engine=MyISAM character set gbk collate gbk_chinese_ci; 

3. The Connection object connecting to the database in the program needs to be written

 
 
  1. Connection con = DriverManaager.getConnection("jdbc:mysql:
    //...user=..&password=...&useUnicode=true&characterEncoding=gbk"); 

4. if you use the mysql command on the terminal to insert data to the database, the command to enter the mysql database is written as: # mysql -- default-character-set = gbk-u... -p

5. Set the encoding on the jsp page to UTF-8.

I found a way to solve this problem.

When installing mysql server, it doesn't matter whether you select gb2312 or UTF-8. These character sets can work well in phpMyAdmin, mainly because your scripts must correctly display Chinese characters, you can run the following statement in a php script after you connect to the database and submit the data query:

$ SetCharacterSetSql = "set names 'character SET of your own '";

 
 
  1. $Recordset1 = mysql_query($SetCharacterSetSql, $conn_Resource) or die(mysql_error()); 

Then execute your own query statement. The result is correct. I have just tried gb2312 and UTF-8, and there is no problem.

The above content is an introduction to some MySQL database Chinese garbled solutions. I hope you will have some gains.

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.