Home > Article > Backend Development > What to do if php mysql is garbled
php Mysq garbled solution: 1. Modify the database encoding through the "Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin" statement; 2. Pass "header("content-type:text/html; charset =utf-8")" statement sets the page character set.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php What should I do if mysql is garbled?
php mysql is garbled solution
1. The default encoding of mysql database is utf8 , if this encoding is inconsistent with your PHP web page, it may cause MYSQL garbled code.
Modify the database encoding. If the database encoding is incorrect, you can execute the following command in phpmyadmin:
Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
The above command is to set the encoding of the test database to utf8.
2. 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.
Modify the encoding of the table:
Alter TABLE ‘category’ DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
The above command is to change the encoding of a table category to utf8.
3. You can choose the encoding when adding fields when creating a table in MYSQL , if this encoding is inconsistent with your web page encoding, it may also cause MYSQL garbled code.
Modify the field encoding:
Alter TABLE ‘test’ CHANGE ‘dd’ ‘dd’ VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
The above command is to change the field encoding of dd in the test table to utf8 .
4. 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 this is the case, it is easy to solve, just check the page. Just modify the charset of the source file.
5. 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.
In this case, you can also modify the page charset.
6. The character set of the PHP page is incorrect.
In order to avoid the occurrence of garbled characters on the PHP page, the first sentence of the PHP page
header("content-type:text/html; charset=utf-8"); //强行指定页面的编码,以避免乱码
7. The encoding specified in the PHP statement to connect to the MYSQL database is incorrect.
In the statement to connect to the database.
mysql_connect('localhost','user','password'); mysql_select_db('my_db'); mysql_query("set names 'utf8'"); //select 数据库之后加多这一句
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What to do if php mysql is garbled. For more information, please follow other related articles on the PHP Chinese website!