Home > Article > Backend Development > What to do if php Chinese output is garbled?
## Operating environment for this tutorial: Windows 7 system, php5.6 , this article applies to all brands of computers. Recommended: "Solution to garbled Chinese output in php: 1. Add the code "charset=UTF-8" to the header of the PHP file; 2. Add the code "mysql_query('SET NAMES UTF8" to the previous line of the MySQL query statement ');".
PHP Video Tutorial"
Solution to the problem of PHP outputting Chinese garbled charactersProblem descriptionTo Navigation Dog today The PHP program and database files of (https://daohanggou.cn/) were migrated to the server, but after migrating to the new server, the Chinese output by PHP and the Chinese in the data queried from the MySQL database output by PHP were garbled. situation. Below is a record of my process of solving this problem. Solution stepsThe first solution is the direct output of the PHP program (The output is not the data queried from the database) Chinese garbled problem. Since I am using a virtual host and do not have administrator rights, I cannot modify the 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 add the following code:
<?phpheader("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, what I marked with the red box is the result of the PHP program querying from the MySQL database. The English in the query result can be displayed normally, but the Chinese 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');as shown in 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;
The above is the detailed content of What to do if php Chinese output is garbled?. For more information, please follow other related articles on the PHP Chinese website!