To solve the problem of garbled characters inserted into the Chinese database:
Most of the problems are caused by converting Chinese into UTF-8.
| The code is as follows: |
Copy code |
$ Str = iconv ('gbk', 'utf-8', $ str ); |
If not, check whether the webpage code is correct:
| The code is as follows: |
Copy code |
<? Php Header ("Content-Type: text/html; charset = utf-8 "); ?> |
Example
For example ):
Table creation:
| The code is as follows: |
Copy code |
Create TABLE 'net _ City '( 'Cityid' smallint (4) not null auto_increment, 'Cityname' varchar (80) not null default '', 'Provinceid' smallint (2) not null default '0 ', 'Inregion' varchar (5000) not null default '', 'Outregion' varchar (5000) not null default '', 'Tel 'varchar (400) not null default '', Primary key ('cityid ') ) ENGINE = MyISAM default charset = utf8; PHP code: $ Conn = mysql_connect ("localhost", "username", "password "); Mysql_query ("set names 'utf8'", $ conn ); Mysql_select_db ("www.111cn.net data name", $ conn ); $ Exec = "insert into net_city (cityname, inarea, outarea, tel) values ('". $ link_cityname. "','". $ link_inarea. "','". $ link_outarea. "','". $ link_tel. "')"; $ Result = mysql_query ($ exec, $ conn ); If ($ result ){ Echo "1 "; } Else { Echo "0 "; } Mysql_close ($ conn ); |
Later, I tried to use all of them as gbk, which is also acceptable ~
When querying data, we directly use mysql_query () to set
Mysql_query ("set names gbk"); // encoding is SET at GBK.
Example
The code for the "insert. php" page is as follows:
| The code is as follows: |
Copy code |
<? Php $ con = mysql_connect ("localhost", "peter", "abc123 "); If (! $ Con) {die ('could not connect: '. mysql_error ());} Mysql_select_db ("my_db", $ con ); Mysql_query ("set names gbk"); // encoding is SET at GBK. $ SQL = "INSERT INTO person (FirstName, LastName, Age) VALUES ('$ _ POST [firstname]', '$ _ POST [lastname]', '$ _ POST [age]') "; If (! Mysql_query ($ SQL, $ con )) {Die ('Error: '. mysql_error ());} Echo "1 record added "; Mysql_close ($ con) ?> |