Solve php mysql Query insert Chinese garbled problem

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

The problem with Chinese is that the code is unified or it will be garbled.

1. Database and PHP page coding Unified

2. Database Data table field and page encoding unification

If you do not have the above two kinds of Chinese garbled problem, then we look at MySQL Chinese garbled problem solving examples

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 ' landing 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 the above are UTF8 encoded so that I can 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 can get into the normal, and some people say no. In fact, the key is not in the SQL statement, the focus is PHP connection to MySQL data encoding settings, as follows.

Page edits as GBK

The code is as follows

<?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 the connection to the database submitted must be garbled or saved not carried out, if you want to solve this MySQL Chinese garbled problem is very simple in the query

The code is as follows

<?php
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);

Then you will find that even if your page is GBK, the data saved will be successful.

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.