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

What to do if php symbols are garbled

藏色散人
Release: 2023-03-16 22:56:01
Original
1890 people have browsed it

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.

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>
Copy after login

Or you can also add the following code:

<?php
header("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, 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;);
Copy after login

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;
Copy after login

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!

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