Solve php mysql Query insert Chinese garbled problem _php tutorial

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

Solve php mysql Query insert Chinese garbled problem


First, the problem of Chinese is the unified code, otherwise it will be garbled

1. Database and PHP page encoding Unified

2. Database Data table field and page encoding Unified

If you do the above two, there is no Chinese garbled problem, then we see MySQL Chinese garbled problem solved example

1. My MySQL table is as follows

--

--The structure of the table ' Useradmin '

--

The code is as follows

CREATE TABLE IF not EXISTS ' Userain ' (
' ID ' int (4) not NULL auto_increment,
' username ' varchar DEFAULT NULL,
' Userpass ' varchar (+) DEFAULT NULL,
' Logins ' int (4) not NULL DEFAULT ' 0 ' COMMENT ' number of landings ',
' Logintime ' timestamp not NULL the 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 UTF8 encoded, so I'm going to test a Chinese

The code is as follows

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

I said I could get in, and some people say it's definitely not. The key is not the SQL statement, the focus is PHP connection MySQL data encoding settings, as follows.

Page edit as GBK

code as follows

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

?>

Connection to the database after commit is definitely garbled or save not carried out, if you want to solve this MySQL Chinese garbled problem is very simple in the query at the

code as follows

Configure MySQL Database connection parameters
$db = mysql_connect (www.45it.net, "User", "password");
mysql_select_db ("message", $db);
Add the following line before executing the SQL statement
mysql_query ("SET NAMES ' UTF8 '", $db);

This way, you will find that even if your page is GBK, the submitted data will be saved successfully.

PS: For Ajax we must use UFT8, because Ajax only supports UFT8 mode to transfer data.

http://www.bkjia.com/PHPjc/928039.html www.bkjia.com true http://www.bkjia.com/PHPjc/928039.html techarticle solve php mysql query insert Chinese garbled problem one. The Chinese problem is that it is encoded by the unified code, otherwise it will be garbled 1. Database and PHP page encoding unified 2. Database data table fields and pages ...

  • 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.