mysql+php generate messy code cause

Some mysql database default encoding is utf8, if this encoding is not consistent with your PHP web page, it may cause MYSQL mess.

When you create a table in MYSQL, you will be asked to choose an encoding, if this encoding is not the same as your web encoding, it may also cause MYSQL to mess up.

MYSQL can choose the encoding when creating a table, if this encoding is not consistent with your web page encoding, it may also cause MYSQL mess.

The code of the page submitted by the user is not the same as the code of the page displaying the data, which will definitely cause the PHP page to be garbled.

If the page where user input information is big5 code, the page which shows user input is gb2312, this 100% will cause PHP page mess.

PHP page character set is incorrect.

PHP connection to MYSQL database statement specifies incorrect encoding.


Many people suspect that the inconsistency of mysql version will lead to messy code, I believe that after reading this note you will not think so.

Usually the text you see in some websites may have several codes, such as you see a traditional Chinese character, it may be big5 code, it may be utf-8 code, it may be gb code, yes, that means there are traditional Chinese characters with simplified code, there are also simplified Chinese characters with traditional code, be sure to understand this.

If you are doing a simplified encoding of the web page, the code set to GB2312, if there are visitors from Hong Kong and Taiwan to submit information in traditional Chinese, it may cause garbled code, the solution:

Set the site encoding to utf-8, so that it is compatible with all characters in the world,

If the website has been running for a long time and there is a lot of old data, so you cannot change the setting of Simplified Chinese, then it is recommended to set the encoding of the page to GBK, the difference between GBK and GB2312 is: GBK can display more characters than GB2312, to display traditional Chinese characters in simplified code, you can only use GBK.

The reasons for the messy code generated by using mysql+php are well understood, so it is not difficult to solve.

The solution of mysql+php generating garbled code:

If the installation of mysql code can not be changed, many friends are buying web hosting to build a website, no right to change the MYSQL installation code, this level we can skip, because as long as the latter step gathered correctly, the same can solve the problem of garbled code

If the database encoding is not correct, you can execute the following command in phpmyadmin:


The above command is to set the encoding of the test database to utf8.

Modify the code of the table.


The above command is to change the encoding of a table category to utf8.

Modify the code of the field.


The above command is to change the field encoding of dd in the test table to utf8.

If this is the case, it is easy to solve, just check the page and change the charset of the source file.

In this case, you can also change the page charset.

In the connect to database statement.



mysql_query(“set names utf8;”); //select database after adding this sentence

In order to avoid the occurrence of PHP page mess, the first sentence at the beginning of the PHP page

header(“content-type:text/html; charset=utf-8”); // Force the encoding of the page to avoid garbled code


For example, if the data submitted by your user is BIG5, it is impossible to change it to GB2312 by the above method.

Leave a Reply

Your email address will not be published. Required fields are marked *