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