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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
