What to do if php Chinese output is garbled?

藏色散人
Release: 2023-03-07 17:20:02
Original
2910 people have browsed it

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 ');".

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: "

PHP Video Tutorial"

Solution to the problem of PHP outputting Chinese garbled characters

Problem description

To 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 steps

The 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. :

1

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>

Copy after login
Or you can add the following code:

1

<?phpheader("Content-type:text/html;charset=utf-8");?>

Copy after login
After processing in this way, the output result is like this (Figure 1):

图 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:

1

mysql_query('SET NAMES UTF8');

Copy after login
as shown in Figure 2:

图 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:

1

ALTER TABLE `Test` DEFAULT CHARACTER SET utf8;

Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!