MYSQL database USES es1EN 8 Chinese encoding garbled solution

  • 2020-11-26 19:01:22
  • OfStack

1. Create databases and data tables with phpmyadmin
When creating a database, set collate to: utf8_general_ci
Or execute a statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 

When creating the data table: If the field is in Chinese, set "collate" to "utf8_general_ci".
If the field is in English or Numbers, the default is fine.
The corresponding SQL statement, for example:


CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM ; 

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

$connection = mysql_connect($host_name, $host_user, $host_pass);  

Add two lines:


mysql_query("set character set 'utf8'");// Read the library  
mysql_query("set names 'utf8'");// Write a library  

You can read and write to the MYSQL database normally.
The es31EN-win32-2.5.10 environment is used, and the default utf8 code is used when installing this package.
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're doing the page, notice this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

So no matter input database Chinese, or page display, it is normal.
In DW CS4, utf8 pages are also generated by default.
Similarly, if 1 starts writing the database connection file with:


mysql_query("SET NAMES 'GBK'"); 

The page should also be:

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  

In conclusion, it is very convenient to solve the problem of messy code if the page code is unified by 1, especially in the setting of mysql_query(), set names, which must be counted by 1 with the page and database code.

I hope these two articles on es60EN-8 can help you solve this kind of problem better. I hope you enjoy them.


Related articles: