Preliminary Exploration of garbled Programming

Source: Internet
Author: User

Introduction: This is a detailed page for preliminary exploration of programming Garbled text. It introduces the knowledge, skills, and experience related to PhP, PHP, MySQL, and Garbled text programming, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 344668 'rolling = 'no'>

[Sharing] Preliminary Exploration of garbled Programming

Author: Ma shunren

I believe many of my friends are writingProgramSo today I will discuss this issue with you.

.

Garbled text is divided into many types. Many people start to scold MySQL when they see Garbled text. In fact, in many cases it is not created by MySQL.

.

From the analysis of the causes of garbled data, there are generally the following situations.

1. inconsistency between webpage encoding and program Encoding

The so-called webpage encoding problem refers to your HTMLCodeFor example, the following code:

1  

He sets the browser to use the gb2312 character set. At this time, if the program uses utf8 encoding (that is, what

Code, which is usually set in the editor), then garbled code will be generated.

The solution is to change the charset value to utf8.

2. garbled code

In this case, your code is garbled. This is generally because you use another encoding format.

After modifying your code and saving it, your program will have several encoding forms of code, so the output will naturally

There will be garbled characters. This type of problem is relatively easy to solve, as long as you manually find the garbled code and then modify it (very

Multi-editor can do this ).

3. MySQL issues (this discussion only covers Versions later than mysql5.0)

Now let's take a look at the problem that is most likely to cause gibberish and also the most confusing MySQL gibberish problem.

To describe MySQL garbled characters, you must first start with MySQL parameters. From mysql5, there are several more system changes with character sets.

Quantity:

Character_set_client client Character Set

Character_set_connection character set used for client-to-server connection

Character_set_results select Character Set of the returned data

Character_set_database character set used by the database

Garbled text is generally caused by incorrect settings of the above variables. Many people generally ask Garbled text when asking questions.

The following is the answer: "set names first ".

So what is set names? Set names actually sets character_set_client at the same time,

Character_set_connection, character_set_results.

For example, set names 'gbk' is equivalent:

Set @ character_set_client = 'gbk' set @ character_set_connection = 'gbk' set @ character_set_results = 'gbk'

In many cases, the garbled problem can be solved after this setting. However, we still cannot completely avoid the possibility of garbled characters,

Why?

Because the character_set_client and character_set_connection variables only use and guarantee

Character_set_database encoding is consistent, while character_set_results uses and ensures that the results returned by the SELECT statement are consistent

The program code is consistent.

For example, if your database (character_set_database) uses the utf8 character set, you must ensure that

Character_set_client and character_set_connection are also utf8 character sets. And your program may adopt

Not utf8. For example, if your program uses GBK, if you set character_set_results to utf8

Garbled characters. In this case, set character_set_results to GBK. This ensures that the results returned by the database and

The encoding of your program is the same.

Below is a piece of code for setting character sets (I used a self-written dB library, I believe it should not affect reading ).

Read ):

<? // Assume that our program uses the utf8 character set $ program_char = 'utf8'; // first check the mysql version number, if the version number is greater than 4, we can set these system variables (mysql4 does not have these system variables) $ version = Current ($ db-> fetch_one ('select version ()')); if (substr ($ version, 0, 1)> 4) {// retrieve the character set of the current database $ SQL = 'select @ character_set_database '; $ char = Current ($ db-> fetch_one ($ SQL); // set the client character set (character_set_client) and connection character set (character_set_connection) to the character_set_database) consistent $ db-> query ('set characte R_set_client = "'. $ char. '"'); $ db-> query ('set @ character_set_connection = "'. $ char. '"'); // set the character set of the data returned by the SELECT query to be consistent with the character set of the current program $ db-> query ('set @ character_set_results = "'. $ program_char. '"') ;}?>

At this point, we should be able to solve most of the garbled issues we encounter. In addition, we must also emphasize that sometimes garbled

It may be caused by a combination of the above reasons.

It is troublesome to solve such problems. Because the character set and

The character set of the database is consistent. If you store GBK character data in a utf8 database (If your page Encoding

If the encoding is inconsistent with that of the database, this problem may occur.) If the preceding settings are correct, the result is:

Garbled characters still occur. However, once the cause of such problems is determined, there is still a solution, such

In this example.

If we can ensure that the settings for other links are correct, and we can confirm that the GBK data is stored in utf8.

The garbled characters in the database, so I can extract the data from the database and then use iconv () to convert the encoding.

Yes.

All in all, we should try our best to ensure that the data in the database is correct, so it is relatively simple to handle the problem.

After talking a lot about it, you may be confused. I will summarize it into the following four points for your convenience.

1. Ensure that the data stored in the database is consistent with the database encoding, that is, the data encoding is consistent with character_set_database;

2. Ensure that the character set for communication is consistent with the character set of the database, that is, character_set_client,

Character_set_connection is consistent with character_set_database;

3. Ensure that the returned results of select are encoded in the same way as those of the program, that is, character_set_results is encoded in the same way as the program;

4. Ensure that the program code is consistent with the browser code, that is, the program code and <meta http-equiv = "Content-Type"

Content = "text/html; charset =? "/> Consistent.

Defend your dreams! Address: http://www.cnblogs.com/c3055/archive/2011/08/13/2137455.html

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/344668.html pageno: 4.

Author: Ma shunren

I believe many of my friends have been harassed by garbled characters during program writing. I will discuss this issue with you today.

.

Garbled text is divided into many types. Many people start to scold MySQL when they see Garbled text. In fact, in many cases it is not created by MySQL.

.

From the analysis of the causes of garbled data, there are generally the following situations.

1. inconsistency between webpage encoding and program Encoding

The so-called webpage encoding problem refers to the "charset" defined in your HTML code, such as the following code:

1  

He sets the browser to use the gb2312 character set. At this time, if the program uses utf8 encoding (that is, what

Code, which is usually set in the editor), then garbled code will be generated.

The solution is to change the charset value to utf8.

2. garbled code

In this case, your code is garbled. This is generally because you use another encoding format.

After modifying your code and saving it, your program will have several encoding forms of code, so the output will naturally

There will be garbled characters. This type of problem is relatively easy to solve, as long as you manually find the garbled code and then modify it (very

Multi-editor can do this ).

3. MySQL issues (this discussion only covers Versions later than mysql5.0)

Now let's take a look at the problem that is most likely to cause gibberish and also the most confusing MySQL gibberish problem.

To describe MySQL garbled characters, you must first start with MySQL parameters. From mysql5, there are several more system changes with character sets.

Quantity:

Character_set_client client Character Set

Character_set_connection character set used for client-to-server connection

Character_set_results select Character Set of the returned data

Character_set_database character set used by the database

Garbled text is generally caused by incorrect settings of the above variables. Many people generally ask Garbled text when asking questions.

The following is the answer: "set names first ".

So what is set names? Set names actually sets character_set_client at the same time,

Character_set_connection, character_set_results.

For example, set names 'gbk' is equivalent:

Set @ character_set_client = 'gbk' set @ character_set_connection = 'gbk' set @ character_set_results = 'gbk'

In many cases, the garbled problem can be solved after this setting. However, we still cannot completely avoid the possibility of garbled characters,

Why?

Because the character_set_client and character_set_connection variables only use and guarantee

Character_set_database encoding is consistent, while character_set_results uses and ensures that the results returned by the SELECT statement are consistent

The program code is consistent.

For example, if your database (character_set_database) uses the utf8 character set, you must ensure that

Character_set_client and character_set_connection are also utf8 character sets. And your program may adopt

Not utf8. For example, if your program uses GBK, if you set character_set_results to utf8

Garbled characters. In this case, set character_set_results to GBK. This ensures that the results returned by the database and

The encoding of your program is the same.

Below is a piece of code for setting character sets (I used a self-written dB library, I believe it should not affect reading ).

Read ):

<? // Assume that our program uses the utf8 character set $ program_char = 'utf8'; // first check the mysql version number, if the version number is greater than 4, we can set these system variables (mysql4 does not have these system variables) $ version = Current ($ db-> fetch_one ('select version ()')); if (substr ($ version, 0, 1)> 4) {// retrieve the character set of the current database $ SQL = 'select @ character_set_database '; $ char = Current ($ db-> fetch_one ($ SQL); // set the client character set (character_set_client) and connection character set (character_set_connection) to the character_set_database) consistent $ db-> query ('set characte R_set_client = "'. $ char. '"'); $ db-> query ('set @ character_set_connection = "'. $ char. '"'); // set the character set of the data returned by the SELECT query to be consistent with the character set of the current program $ db-> query ('set @ character_set_results = "'. $ program_char. '"') ;}?>

At this point, we should be able to solve most of the garbled issues we encounter. In addition, we must also emphasize that sometimes garbled

It may be caused by a combination of the above reasons.

It is troublesome to solve such problems. Because the character set and

The character set of the database is consistent. If you store GBK character data in a utf8 database (If your page Encoding

If the encoding is inconsistent with that of the database, this problem may occur.) If the preceding settings are correct, the result is:

Garbled characters still occur. However, once the cause of such problems is determined, there is still a solution, such

In this example.

If we can ensure that the settings for other links are correct, and we can confirm that the GBK data is stored in utf8.

The garbled characters in the database, so I can extract the data from the database and then use iconv () to convert the encoding.

Yes.

All in all, we should try our best to ensure that the data in the database is correct, so it is relatively simple to handle the problem.

After talking a lot about it, you may be confused. I will summarize it into the following four points for your convenience.

1. Ensure that the data stored in the database is consistent with the database encoding, that is, the data encoding is consistent with character_set_database;

2. Ensure that the character set for communication is consistent with the character set of the database, that is, character_set_client,

Character_set_connection is consistent with character_set_database;

3. Ensure that the returned results of select are encoded in the same way as those of the program, that is, character_set_results is encoded in the same way as the program;

4. Ensure that the program code is consistent with the browser code, that is, the program code and <meta http-equiv = "Content-Type"

Content = "text/html; charset =? "/> Consistent.

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.