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.