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.