In the process of using PHP to connect to the database, it is easy to encounter garbled characters. This is caused by incorrect character set settings when PHP communicates with the database. The following methods can be used to solve this problem.
SHOW VARIABLES LIKE '%character_set_database%';
If it is not set to utf8 encoding, you need to change it to utf8. Use the following statement to modify the database character set:
ALTER DATABASE [数据库名称] CHARACTER SET utf8;
header('Content-Type:text/html;charset=utf-8');
mysqli_set_charset($link, 'utf8');
where $link is the database link object.
mysqli_query($link, "SET NAMES 'utf8'");
where $link is the database link object.
To sum up, if the PHP link database has garbled characters, it can be solved by the above method. It should be noted that the above methods need to be used with caution to avoid data damage caused by incorrect modifications. It is recommended to test in the test environment first, and then apply it to the production environment after confirming that it is correct.
The above is the detailed content of PHP link database is garbled. For more information, please follow other related articles on the PHP Chinese website!