Mysql inserts Chinese garbled characters

Source: Internet
Author: User
Mysql inserts a Chinese garbled php file and sets the webpage encoding to UTF-8.
Header ("Content-type: text/html; charset = utf-8 ");

Before the database is inserted, the encoding passed to the database is set to UTF-8.
$ This-> mysqli-> query ("set names utf8 ");

Some character set-related variables are set in the database.
Query show variables like 'char % ', and the result is


Query show variables like 'collation _ % ', and the result is


I used WampServer to modify the mysql configuration file my. ini: add default-character-set = utf8 under the [client] Node, add character_set_server = utf8 under the [mysqld] node, and restart the musql service.


However, after being passed to the database, Chinese characters are converted into question marks .. How can I fix garbled text?


Reply to discussion (solution)

Restore all your modifications to the initial state!

Note that:
1. all settings related to character sets do not affect existing data in the database.
2. character set-related settings, only apply to the data inserted after the settings
3. the database is a shared resource. do not modify the settings easily. To avoid other application problems

As long as the following two points are done, no garbled problem will occur
1. all fields that may contain Chinese content must be connected and proofread using the Chinese character set. It doesn't matter which character set (both gbk, utf8, and ucs2 can be used), and every field does not need to be the same.
2. execute the set name page character set once after each connection to the database.

I just deleted the garbled data from the data table after setting the database character set, and then executed the program. The inserted Chinese characters are still garbled.

Because I used a test database, I modified the character set settings. I can't remember what the default configuration is. how can I recover it...

After each connection to the database, I run $ this-> mysqli-> query ("set names utf8"); should it be okay?
All fields that may contain Chinese content must be connected to the Chinese character set for verification. This field is too white, meaning show variables like 'collation _ % '; is the result displayed after the statement is executed?

Thank you for your patience.

Database connection tool

 Mysqli = new Mysqli (self: $ host, self: $ user, self: $ pwd, self: $ db ); if ($ this-> mysqli-> connect_error) {die ("database connection failed ". $ this-> mysqli-> connect_error);} $ this-> mysqli-> query ("set names utf8");} public function execute_dql ($ SQL) {$ res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error); return $ res ;} public function execute_dml ($ SQL) {$ res = $ this-> mysqli-> query ($ SQL) or die ($ this-> mysqli-> error); if (! $ Res) {return 0;} else {if ($ this-> mysqli-> affected_rows> 0) {return 1 ;}else {return 2 ;}}}?>


What is the character set of a table? It should not affect your save results ~

What is the character set of a table? It should not affect your save results ~

How to view the character set of a table? I use the default character set. for the modified configuration, see the first floor.

The name of the shou create table. you can also view the table by using the phpmyadmin graphical tool.

Indicates the name of the show create table;

Create table 'TB _ userinfo '(
'Id' int (20) not null AUTO_INCREMENT,
'Username' varchar (20) default null,
'Password' varchar (20) default null,
'Email 'varchar (20) default null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 90 default charset = latin1



It's latin1. how can I change it to utf8?

Create table 'TB _ userinfo '(
'Id' int (20) not null AUTO_INCREMENT,
'Username' varchar (20) default null,
'Password' varchar (20) default null,
'Email 'varchar (20) default null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 90 default charset = latin1



It's latin1. how can I change it to utf8?

Go to phpmyadmin to modify

Delete original table
Drop table tb_userinfo

Recreate
Create table 'TB _ userinfo '(
'Id' int (20) not null AUTO_INCREMENT,
'Username' varchar (20) default null,
'Password' varchar (20) default null,
'Email 'varchar (20) default null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 90 default charset = utf8

You don't need to do this.
You only need to modify the connection verification.
Alter table 'TB _ userinfo' CHANGE 'username' VARCHAR (20) character set utf8 COLLATE utf8_general_ci NOT NULL

If the table character set is changed and the field needs to be changed, you can drop the table and recreate the table.

Create table 'TB _ userinfo '(
'Id' int (20) not null AUTO_INCREMENT,
'Username' varchar (20) default null,
'Password' varchar (20) default null,
'Email 'varchar (20) default null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 90 default charset = utf8;

Pay attention to backing up data. In fact, if you are not worried about using phpmyadmin to view garbled characters, you only need to set character sets when php is connected to the database, without affecting data access... If it is normal, do not mount phpmyadmin on the website as soon as possible, so as to avoid problems ~~

Drop and recreate the table. now the table's character set is utf8.
However

$ SQL = "insert into tb_userinfo (username, password, email) values ('sss Li Si', '123456', 'Lis @ sina.com ')";

After insertion, only sss is displayed. if it is a pure Chinese character, nothing can be inserted.

Saving php files as UTF-8 without bom format

Thanks for the success. I am practicing now. the php document created in Notepad is encoded as ANSI by default.

But is it so troublesome to set it every time?
I feel that mysql is difficult to support Chinese characters. I may have changed some of them without modifying them. I don't know which configurations must be modified?

Refer to the reply from version #1, my. you don't need to change the parameters in ini. you just need to set names utf8; (of course, if your table is gbk, it will be gbk here ), also, ensure that the encoding of the inserted data is consistent with that in the database.

UTF-8 is used because it can only use UTF-8 in linux.
In Windows, the default value is gbk. isn't it difficult to create UTF-8?

Some people say that UTF-8 is used for internationalization. how many foreigners can understand square characters?

Thank you again for closing the post.

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.