Cause analysis and solution for MySQL + PHP garbled characters

Source: Internet
Author: User

◆ The default encoding of the mysql database tutorial is utf8. If this encoding is inconsistent with your php webpage, it may cause mysql garbled characters;

◆ When creating a table in mysql, you will be asked to select an encoding. If this encoding is inconsistent with your webpage encoding, mysql may also be garbled;

◆ You can select encoding to add fields when creating a mysql table. If this encoding is inconsistent with your webpage encoding, mysql may also be garbled;

◆ If the encoding of the page submitted by the user is inconsistent with the page encoding of the displayed data, it will certainly cause php page garbled characters;

◆ If the user input data page is big5, the user input page is gb2312, which may cause php page garbled characters;

◆ Incorrect php page character set;

◆ The encoding specified by the php connection to the mysql database statement is incorrect.


Note:

Many people suspect that inconsistent mysql versions may cause garbled characters. I believe you will not think so after reading this description.

The text you see on some websites may contain several encodings. If you see a traditional Chinese character, it may be big5 or UTF-8 encoded, it is more likely to be a gb code. That is to say, there are simplified Chinese characters and simplified Chinese characters. You must understand this.

If you are creating a simplified code webpage with the code set to gb2312, if visitors from Hong Kong and Taiwan submit traditional Chinese information, it may cause garbled characters. Solution:

Set the website encoding to UTF-8 to be compatible with all characters in the world.

If the website has been running for a long time and has a lot of old data, you cannot change the settings of simplified Chinese, we recommend that you set the page encoding to gbk. The difference between gbk and gb2312 is: gbk can display more characters than gb2312. To display simplified Chinese characters, you can only use gbk.

The reasons for using mysql + php to produce garbled characters are clearly understood, so it is not difficult to solve the problem.

Solution to mysql + php garbled code generation:

If the encoding for installing mysql cannot be changed, many of our friends purchase a virtual host to create a website and do not have the right to change the encoding for installing mysql. We can skip this step as long as the subsequent steps are correct, the same can solve the garbled problem.

Modify the database encoding. If the database encoding is incorrect, run the following command in phpmyadmin:

SQL code
   
01. alter database 'test' default character set utf8 collate utf8_bin
The preceding command sets the encoding of the test database to utf8.


Modify the table encoding:

SQL code
   
01. alter table 'category' default character set utf8 collate utf8_bin
The preceding command changes the category encoding of a table to utf8.

Modify the field encoding:

SQL code
   
01. alter table 'test' change 'DD' DD' varchar (45) character
   
02. set utf8 collate utf8_bin not null
The preceding command changes the dd field encoding in the test table to utf8.

If this is the case, you only need to check the page and modify the charset of the source file.

In this case, modify the charset page.

In the database connection statement.

SQL code
   
01. mysql_connect ('localhost', 'user', 'password ');
   
02. mysql_select_db ('My _ db ');
   
03. mysql_query ("set names utf8;"); // add this sentence after the select database
To avoid php page garbled characters, the first sentence of the php page begins.

 

Php code
   
01. header ("content-type: text/html; charset = utf-8 ");//
Forcibly specify the page encoding to avoid garbled characters

Note: after the preceding method is modified, you can only ensure that the newly inserted data is not garbled. For example, if the data you have submitted is big5, but you want to use the above method to make it impossible to display the code correctly on the gb2312 webpage. This kind of text internal code conversion can only be solved by another write program.

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.