The ultimate solution to garbled data display in PHP database operations

The php connection to mysql is garbled during the development process. This is a problem often encountered by novices in development. Based on the actual problems encountered by everyone, the problem of garbled characters has been summarized into 9 key points to completely solve the problem of garbled characters after connection.

The core idea to solve the problem of garbled characters is: the encoding must be unified in multiple different file systems.

These 9 key points are:

1.html coding is consistent with MySQL coding

2.PHP coding is consistent with MySQL coding

3.If If there is a header to send the character set, please use the same character set as the database

4. To be consistent with the page The character encoding of the database must be consistent

5. The character set of the database must be unified

6. The character set of the table must be unified

7. The character set of the column must be unified (table If set, the column will be written to the table by default)

8. The character set for connection and verification must be unified

9. The character set of the result set must be unified

1. File encoding

Encoding of html and PHP files, in the example: use notepad++ to demonstrate. Set both PHP and html to this character set.

Must be set to utf-8 without BOM format.

1.png

2. Header encoding

If there is a header in php, it must be utf-8

header('content-type:text/html;charset=utf-8');

3. Web page header file encoding

If the html file encoding is utf-8, it must also be set to one setting

4. Coding to create database

2.png

##5. Table and column creation The table is utf-8

3.png


6. Connection, result, verification character set setting

Come to MySQL through mysqli_set_charset('utf8') Character set settings for connections, results, and validation.

Note: The character set declaration of the database is slightly different from that in the file. UTF8 is for the MySQL database, and UTF-8 is used in files.


Continuing Learning
||
header('content-type:text/html;charset=utf-8'); >
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!