Mysql database using UTF-8 Chinese code garbled solution _mysql

Source: Internet
Author: User

1. Create databases and datasheets with phpMyAdmin
When you create a database, set the collation to: "Utf8_general_ci"
Or execute the statement:

Copy Code code as follows:
CREATE DATABASE ' dbname ' DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

When you create a datasheet: If the field is in Chinese, you need to set the collation to: "Utf8_general_ci",
If the field is stored in English or a number, the default is OK.
The corresponding SQL statement, for example:

CREATE TABLE ' test ' ( 
' id ' INT NOT NULL, 
' name ' VARCHAR "CHARACTER SET UTF8 COLLATE utf8_general_ci NOT null , 
PRIMARY KEY (' id ') 
) ENGINE = MYISAM; 

2. Read and write database in PHP
After connecting to the database:

Copy Code code as follows:
$connection = mysql_connect ($host _name, $host _user, $host _pass);

Add two lines:

mysql_query ("Set character set ' UTF8 '");/Read library 
mysql_query ("Set names ' UTF8 '");//write Library 

You can read and write the MySQL database normally.
Use the appserv-win32-2.5.10 to do the environment, when loading this package with the default UTF8 encoding.
When writing a database connection file, write:

$conn = mysql_connect ("$host", "$user", "$password"); 
mysql_query ("SET NAMES ' UTF8 '"); 
mysql_select_db ("$database", $conn); 

And then when you do the page, pay attention to this sentence:

Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

So no matter the Chinese of the input database, or the page display, it is normal.
In the DW CS4 Edition, the default generation is also the UTF8 page.
Similarly, if you start by writing a database connection file:

 
 

The page should also be changed accordingly:

Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

Summary, finally, the main page coding to be unified can be very convenient to solve the garbled problem, especially in mysql_query () the Set names settings must and the page and the database code statistics one can.

Hope that these two articles on UTF-8 Chinese code garbled article, can better help us solve this kind of problem, I hope you like.

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.