UTF8 Chinese PHP programming _ PHP Tutorial

Source: Internet
Author: User
PHP programming in UTF8. To be honest, sandal also thinks UTF8 is a good thing ...... After all, the screen shows that China, Japan, and South Korea are very attractive to East Asians ...... (The benefit is not just that ......) Preface:

To be honest,
Sandals also think UTF8 is a good thing ......
After all, the screen shows that China, Japan, and South Korea are very attractive to East Asians ......
(The benefit is not just that ......)
Not only webpage programs ......
Many applications use Unicode encoding in their kernels ......
The purpose is obvious: Support for multi-language display ......
All Microsoft software is a Unicode kernel ......
Therefore, the Japanese software can be properly displayed on your Chinese XP ......
The Chinese 98 will cause garbled characters when the GB kernel is installed with other language software ......

For UTF8,
It can be said that it is a branch of Unicode,
It stores a Chinese character in three bytes ......
(Unicode uses four bytes)
Application software has all been thrown to Unicode ......
Cannot we use UTF8 for webpage programs?

In this article, I will try my best to introduce php (as the mainstream development language) programming under UTF8 encoding ......
Why do we separately introduce "Chinese "......
This is because UTF8 ......
Unless you are preparing a multi-language system ......
(I want to complain: foreigners do not pay attention to this issue when writing programs ......)
Second, the UTF8 encoding methods for multi-byte languages such as China, Japan, and South Korea are similar ......
Just move on to the menu ......
Good ...... Let's start with the database processing ......


========================================================== =
Connect to database

Many people will find data out of chaos when they upgrade to MySQL (the best combination with PHP) 4.1 ......
It is because MySQL (the best combination with PHP) has supported character sets since 4.1 ......
The default character set is UTF8 ......
(Fully prove the importance of integration with international standards ...... Hey hey ......)
In the past, most of us used utf8 or GBK encoding ......
The output data is garbled ......
Fix garbled characters ......
You have to let the program know what encoding data to get ......

Assume that your previous database is UTF-8 encoded ......
You can add a statement before the query.

MySQL (best combination with PHP) _ query (set character set utf8) or die ("Query failed :". mySQL (the best combination with PHP) _ error ());
Of course, this is the case only when more than 4.1 of the requests are processed,
Therefore, we can add the following judgment:

$ MySQL (the best combination with PHP) version = $ db-> query_first ("select version () AS version ");
If ($ MySQL (the best combination with PHP) version [version]> = 4.1)
{
MySQL (best combination with PHP) _ query (set character set utf8) or die ("Query failed :". mySQL (the best combination with PHP) _ error ());
}
In this way, no matter what the default encoding of MySQL (the best combination with PHP) is, it can be normally accessed ......
(No matter whether you are currently in use, whether you are in service or on a regular basis, or even in the case of zero storage, there is no problem ......)

However, they all go global ......
Are you still using utf8?
How to transcode it?
And ......
What if garbled characters occur during data upgrades?
Cool!
Listen to the next decomposition ......

========================================================== ====
Data upgrade to 4.1

Upgrade ......
Export ......
It's really irresponsible to say that foreigners ......
Some Chinese characters were always lost in the previous export method ......
For example, make "I love you" into "I love you ......
(Usually the last word of a piece of data is lost)
The whole thing went wrong ......
(In the words of pomegranate elder sister, it is "such a difficult task is really exciting "......)
To protect your fragile heart ......
To safeguard the traditional Chinese ethics and morality ......
You can change the fields whose data contains Chinese characters to Binary encoding ......
Specific method ......
You can run the following statement:

Alter table 'table name' convert to character set binary;
In this way, the character type fields, such:
CHAR, VARCHAR, and TEXT
Convert
BINARY, VARBINARY, and BLOB
Then export and import the data to the 4.1 environment ......
Of course, the last tedious task is:
You need to change their type back ......

There are upgrades to 4.1 ......
Of course there are also downgrades ......
How to downgrade ???
Sandal, go to the toilet ......
Please go to the next page ......

========================================================== =====
Data downgrade from 4.1

Someone found that the SQL file exported from 4.1 could not be imported into a lower version program ......
The problem is actually very simple ......
In addition, MySQL (the best combination with PHP) has prepared everything for us ......
Add the-compatible parameter during export ......
Assume that your database is UTF-8 encoded ......
And the target database version is 4.0 ......
Write the following command line:

Shell> MySQL (the best combination with PHP) dump -- user = username -- password = password -- compatible = MySQL (the best combination with PHP) 40 -- default-character-set = utf8 database> db. SQL
In this way, the exported SQL file can be smoothly imported to a lower-version database ......

To be honest, sandal also thinks UTF8 is a good thing ...... After all, the screen shows that China, Japan, and South Korea are very attractive to East Asians ...... (The benefit is not just that ......) Not Only...

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.