" to the header of the php file; 2. In MySQL Just add the code "mysql_query('SET NAMES UTF8');" to the first line of the query statement."/> " to the header of the php file; 2. In MySQL Just add the code "mysql_query('SET NAMES UTF8');" to the first line of the query statement.">

Home  >  Article  >  Backend Development  >  What to do if php symbols are garbled

What to do if php symbols are garbled

藏色散人
藏色散人Original
2022-10-18 09:39:341808browse

Solution to garbled php symbols: 1. Add the code "02074d00c138962c2eea889c98e050ce"; 2. Add the code "mysql_query('SET NAMES UTF8');" to the previous line of the MySQL query statement.

What to do if php symbols are garbled

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

Problem description

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.

Solution steps

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

图 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(&#39;SET NAMES UTF8&#39;);

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:

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!

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