mysql tutorial + php tutorial causes analysis and solutions to garbled characters
◆ The default encoding of mysql database tutorial is utf8. If this encoding is inconsistent with your php web page, it may cause mysql garbled characters;
◆ When creating a table in MySQL, you will be asked to choose an encoding. If this encoding is inconsistent with your web page encoding, it may also cause MySQL garbled characters;
◆ You can choose the encoding when adding fields when creating a table in MySQL. If this encoding is inconsistent with the encoding of your web page, it may also cause MySQL garbled characters;
◆ If the encoding of the page submitted by the user is inconsistent with the encoding of the page displaying the data, it will definitely cause the php page to be garbled;
◆ If the page where the user inputs information is in big5 code, but the page where the user input is displayed is in gb2312, this will 100% cause the php page to be garbled;
◆ The character set of the php page is incorrect;
◆ The encoding specified in the PHP connection mysql database statement is incorrect.
Note:
Many people suspect that inconsistent mysql versions will lead to garbled characters. I believe you won’t think so after reading this guide.
Normally, the text you see on some websites may have several encodings. For example, if you see a traditional Chinese character, it may be big5 encoding, it may be utf-8 encoding, or it may be gb encoding. , Yes, that is to say, there are traditional Chinese characters encoded in simplified Chinese, and there are also simplified Chinese characters encoded in traditional Chinese. You must understand this.
If you are making a simplified coded webpage, the code is set to gb2312. If visitors from Hong Kong and Taiwan submit information in traditional Chinese, it may cause garbled codes. Solution:
Set the website encoding to utf-8, which is compatible with all characters in the world.
If the website has been running for a long time and has a lot of old data, and the Simplified Chinese settings can no longer be changed, it is recommended to set the page encoding to gbk. The difference between gbk and gb2312 is that gbk can display more than gb2312 To display the characters in simplified code, you can only use gbk.
Now that we have a clear understanding of the reasons for the garbled code generated when using mysql+php, the solution will not be difficult.
Solution to the garbled code generated by mysql+php:
If the code for installing mysql cannot be changed, many friends buy virtual hosts to build websites and do not have the right to change the code for mysql installation. We can skip this step, because as long as the following steps are correct, the garbled code problem can be solved. .
Modify the database encoding. If the database encoding is incorrect, you can execute the following command in phpmyadmin:
sql code
01.alter database 'test' default character set utf8 collate utf8_bin
The above command is to set the encoding of the test database to utf8.
Modify the encoding of the table:
sql code
01.alter table 'category' default character set utf8 collate utf8_bin
The above command is to change the encoding of a table category to utf8.
Modify the encoding of the field:
sql code
01.alter table 'test' change 'dd' 'dd' varchar( 45 ) character
02.set utf8 collate utf8_bin not null
The above command is to change the field encoding of dd in the test table to utf8.
If this situation is easy to solve, just check the page and modify the charset of the source file.
In this case, you can also modify the page charset.
In the statement connecting to the database.
sql code
01.mysql_connect('localhost','user','password');
02.mysql_select_db('my_db');
03.mysql_query("set names utf8;"); //Add this sentence after selecting the database
In order to avoid the occurrence of garbled characters on the php page, the first sentence of the php page is
php code
01.header("content-type:text/html; charset=utf-8"); //
Forcibly specify the encoding of the page to avoid garbled characters
Note: Modification according to the above method can only ensure that your newly inserted data will not be garbled. For example: If the data submitted by your user is big5, but you want to use the above method to change it to be correct on the gb2312 web page. Display is impossible, and this transformation of text internal codes can only be solved by writing another program.