PHP and MySQL Chinese encoding Solutions

Source: Internet
Author: User
Tags config php php and mysql

Documentation:

 

Sample enviroment:
It's not simle to yours but I think it will work.
Windows Server 2003 simple Chinese edition
Apache 2.2.11
Php-5.2.8-Win32
MySQL-5.1.31
==================

0. Encoding Selection: UTF-8 or gb2312 or GBK.
The UTF-8 can be compatible with ASCII encoding and takes 1 or 2 bytes as needed, relatively small. (Others such as UTF-16 and UTF-32 are fixed dead occupying 2 or 4 bytes)
Gb2312 can only contain over 6000 common words
GBK can be simplified or traditional, with a wide range

1. settings in PHP
In PHP, enable extension = php_mbstring.dll in the PHP. ini file, and modify the relevant encoding configuration of mb_string as needed.
2. MySQL settings:
The MySQL Character Set supports four levels: Server, database, table, and connection ). Therefore, you can set it later.
3. Apache settings:
If it is a UTF-8, do not set, if it is GBK need to set httpd. conf, enable adddefacharcharset
4. Specific operations:
Step 1: Create a database
The default value is Latine's encoding and collection. Because only one field is Chinese, you do not need to modify the default configuration file of the database.
You only need to set this field to gbk_chinese_ci when creating a database.
Step 2: Set charset and encoding for the webpage. If this parameter is not set
Step 3: When establishing a connection
Set character_set_client = 'gbk'
Set character_set_connection = 'gbk'
Set character_set_results = 'gbk'
(This configuration is equivalent to set names 'gbk '.
)

========================================================== ==========

0. Choose an Encoding
You have 3 choises: UTF-8, gb2312 and GBK.
UTF-8 contains askii, and it uses 1 or 2 bytes so it's smaller to transform in web.
But it's contains less Chinese characters. (others like UTF-16 and UTF-32, is fixed to user 2 or 4 bytes)
Gb2312 contains only 6000 more popular characters, such as 'zhu Rongji 'can't be contained.
GBK contains simple Chinese and tranditional Chinese, I think it's pro. My sample is for GBK.

1. config PHP
Modify PHP. ini, and set the line
Extension = php_mbstring.dll
On
. Maybe you need to initialize ate the mb_string.
2. MySQL configration
As MySQL supports defferent charset in 4 layers, the server, the database, the table and connection.
And you have only one table, one field in Chinese, you need not to modify the whole MySQL configration

3. Apache configuration
If you are willing to use UTF-8, you need not to specify ate anything for IT support UTF-8 by default.
If you want to use GBK you need to modify httpd. conf and open the adddefaultcharset

4. Sample steps:
Firstly, create a database
And the database's collection is Latine.
When you create the table, the field, set the field's collection to gbk_chinese_ci

Secondly, set the special pages you want to show the Chinese characters.
Set the charset and encoding

Third, when you build a connection, either in CommandLine or PHP, you need to run the 3 lines script:

Set character_set_client = 'gbk'
Set character_set_connection = 'gbk'
Set character_set_results = 'gbk'

These three lines are equle

Set names 'gbk'

 

 

 

Example:

 

PHP File

 

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Title> test Chinese Database Import </title>
</Head>

<Body>
<Ul>
<? PHP
$ Conn = mysql_connect ("localhost", "root", "sa ");
Mysql_query ("set names 'gbk'"); // The Key is
Mysql_select_db ("test ");
$ SQL = "select * From chinesetest ";
$ Result = mysql_query ($ SQL, $ conn );

While ($ ROW = mysql_fetch_assoc ($ result ))
{
Echo "<li>". $ row ['id']. "-". $ row ["CF"]. "</LI> ";
}

$ Sql2 = "insert into chinesetest (CF) values ('Chinese language insertion test ');";
Mysql_query ($ sql2 );
$ Sql2 = "insert into chinesetest (CF) values ('Please refer to Chinese here ')";
// Echo $ sql2;
Mysql_query ($ sql2 );
Echo mysql_error ();

Mysql_close ();
?>
</Ul>
</Body>
</Html>
Overall solution:

/Files/xxpyeippx/chinese_on_mysql.zip

Table creation statement:

Create Table if not exists 'chinesetest '(
'Id' int (11) not null auto_increment,
'Cf 'varchar (255) Character Set GBK not null,
Primary Key ('id ')
) Engine = MyISAM default charset = Latin1 auto_increment = 10;

 

Related Article

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.