Home > Backend Development > PHP Problem > How to solve the Chinese garbled characters in the database displayed by PHP

How to solve the Chinese garbled characters in the database displayed by PHP

PHPz
Release: 2023-04-21 09:32:14
Original
976 people have browsed it

When using PHP to call the database during development, we often encounter the problem of Chinese garbled characters. This problem is usually caused by the database character set being different from the PHP character set or PHP not correctly parsing the Chinese characters in the database. In this article, we will introduce how to solve the problem of garbled Chinese characters in PHP.

1. Configure the database character set

1.1 View the database character set
Enter the following command on the command line interface to view the current character set of the database:

show variables like 'character%';
Copy after login

1.2 Set up the database Character set
In order to solve the problem of Chinese garbled characters, the database character set needs to be set to utf8. In mysql, you can set it with the following command:

ALTER DATABASE `database_name` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

where database_name is the name of the database you want to set. Here the default character set of the database is set to utf8. In MySQL, utf8 is the most commonly used character set and can support characters in most languages.

1.3 Set the character set of the data table
In mysql, each data table can also set the character set independently. You can set the character set of a table to utf8 through the following command:

ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

where table_name is the name of the data table you want to set. Here the character set of the data table is set to utf8. In this way, all tables in the database will use the utf8 character set.

2. Set the php character set

2.1 Set the php default character set
Open the php.ini file, then find the "mbstring.language" parameter and set its value to "CN ". As shown below:

mbstring.language=CN
Copy after login

2.2 Set the PHP runtime character set
In PHP, you also need to set the runtime character set. You can set the character set of php through the following code:

header('content-type:text/html;charset=utf-8');
Copy after login

This code will set the output character set of php to utf-8.

3. Call Chinese characters in the database

3.1 Database link character set
In PHP, you need to set the character set when linking to the database. The code is as follows:

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('连接数据库失败!');
mysql_select_db($dbname, $conn);
mysql_query("SET NAMES 'utf8'");
Copy after login

Among them, $dbhost, $dbuser, $dbpass, and $dbname are the host address, user, password, and database name of the database respectively. Here we set the database character set to utf8 through the mysql_query() function.

3.2 Set the query character set
If there are Chinese characters in the query, you need to set the query character set. You can set the query character set through the following code:

mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
Copy after login
  1. Test results
    After configuring the database character set and php character set, we can test the Chinese in the database. You can create a table and insert a Chinese character into it, and then view it through the following code:
$result = mysql_query("SELECT * FROM table_name");
while($row = mysql_fetch_array($result)){
    echo $row['field_name'];
}
Copy after login

This way, the Chinese characters can be displayed correctly on the page.

5. Summary

Through the above steps, we can solve the problem of Chinese garbled characters in the PHP display database. In order to display Chinese characters correctly, you need to set both the database character set and the PHP character set to utf8, and set the query character set and link character set correctly. In this way, Chinese characters can be used in development and displayed correctly.

The above is the detailed content of How to solve the Chinese garbled characters in the database displayed by PHP. For more information, please follow other related articles on the PHP Chinese website!

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