Solution to garbled php symbols: 1. Add the code ""; 2. Add the code "mysql_query('SET NAMES UTF8');" to the previous line of the MySQL query statement.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
PHP solves the problem of outputting Chinese Garbled code problem
Today we migrated the PHP program and database files of Navigation Dog (https://daohanggou.cn/) to the server, but migrated them to a new server. After that, the Chinese output by PHP and the Chinese output by PHP from the data queried from the MySQL database were garbled. Below is a record of my process of solving this problem.
The first thing to solve is the problem that the PHP program directly outputs Chinese garbled characters (output is not the data queried from the database). Since I am using a virtual host, I do not have administrator rights and cannot modify PHP. configuration file, therefore, my solution to this problem is to add the following code to the head of the PHP file:
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head>
Or you can also add the following code:
<?php header("Content-type:text/html;charset=utf-8"); ?>
After processing in this way, The output result is like this (Figure 1):
Figure 1
As shown in Figure 1, I use the red box What is marked is the result of the PHP program querying from the MySQL database. The English in the query results can be displayed normally, but the Chinese has turned into a question mark. Regarding this problem, my solution is to add the following code to the previous line of the MySQL query statement:
mysql_query('SET NAMES UTF8');
Figure 2:
Figure 2
If the problem is still not solved after the above steps, you can Try to use the following SQL command to change the data table with Chinese encoding errors to UTF-8 encoding:
ALTER TABLE `Test` DEFAULT CHARACTER SET utf8;
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What to do if php symbols are garbled. For more information, please follow other related articles on the PHP Chinese website!