Solve the problem of Chinese garbled characters inserted in php mysql Query

Source: Internet
Author: User
Tags mysql query php mysql php mysql query

I. The Chinese character is actually encoded in a unified manner. Otherwise, garbled characters will occur.

1. Unified coding of databases and php pages

2. Unified encoding of fields and pages in the database data table

If there is no Chinese garbled problem in the above two methods, let's look at the mysql Chinese garbled problem solution example.

1. My mysql table is as follows:

--
-- Table structure 'useradmin'
--

The code is as follows: Copy code

Create table if not exists 'userain '(
'Id' int (4) not null AUTO_INCREMENT,
'Username' varchar (20) default null,
'Userpass' varchar (40) default null,
'Logins' int (4) not null default '0' comment' login times ',
'Logintime' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
'Mid 'char (1) not null default '0 ',
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 7;

All of the above are UTF-8 encoded so that I can test a Chinese character.

The code is as follows: Copy code

Insert into 'userain '('id',' I am Chinese', 'userpass', 'logins', 'logintime', 'mid ') VALUES
(1, 'admin', '7c1f03139281878059b909c42ccf2f6a ', 0, '2017-04-14 14:20:26', '1 ');

I said that it can be normally accessed. Some people say that it is definitely not true. The key is not the SQL statement. The key is the encoding settings when php connects to mysql data, as shown below.

Edit the page as gbk

The code is as follows: Copy code

<? Php
// Configure mysql database connection parameters
$ Db = mysql_connect ("localhost", "user", "password ");
Mysql_select_db ("message", $ db );
// Add the following line before executing the SQL statement

?>

After connecting to the database, the submission must be garbled or not saved. If you want to solve this mysql Chinese garbled problem, it is very easy to query

The code is as follows: Copy code

<? Php
// Configure mysql database connection parameters
$ Db = mysql_connect (www.111cn.net, "user", "password ");
Mysql_select_db ("message", $ db );
// Add the following line before executing the SQL statement
Mysql_query ("set names 'utf8'", $ db );

In this way, you will find that even if your page is a gbk submitted data, it will be saved successfully.

Ps: We must use uft8 for ajax, because ajax only supports data transmission in uft8 mode.

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.