Solve the problem of Chinese garbled characters in php inserting data into mysql

Source: Internet
Author: User

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)

?>

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.