Home  >  Article  >  Backend Development  >  What to do when php reads Chinese garbled characters

What to do when php reads Chinese garbled characters

藏色散人
藏色散人Original
2020-11-18 09:54:252174browse

Solutions for php to read Chinese garbled characters: 1. Modify the encoding type of the PHP page language itself; 2. Select "UTF8" for the MySQL character set; 3. Change the same editor; 4. Access MySQL programmatically , just add the code "mysql_query("SET NAMES 'GBK'");".

What to do when php reads Chinese garbled characters

The operating environment of this tutorial: windows10 system, php5.6. This article is applicable to all brands of computers.

Recommended: "PHP Video Tutorial"

Causes and solutions to Chinese garbled characters in PHP

Cause:

1. The encoding type of the PHP page language itself is inappropriate. At this time, the Chinese you write directly in the script will definitely be garbled, not to mention the database;

Solution: Select 'UTF8' or ' gb2312', so that the customer's browser will automatically select and display the correct Chinese display. Note: 'UTF8' or 'gb2312' can display Chinese correctly.

2. The encoding type in the database MySQL is incorrect.

Solution: When creating the database, select 'UTF8' for the MySQL character set, and select utf8_general_ci for the MySQL connection proofreading. There will definitely be no problem with the database created in this way for storing Chinese.

Otherwise, your First of all, Chinese is garbled in MySQL, let alone expect it to be displayed correctly in your PHP page.

3. It is related to the usual script editing environment. For example, some content is written by myself in word, some is written in notepad, and some is written in text editors such as editplus and ultraplus. Sometimes I just write Chinese directly in DW.

It’s strange that this doesn’t produce garbled characters.

Solution: Try to use the same editor. If you are copying existing content, it is recommended to use the encoding conversion function in ultraplus to convert it to utf8 or gb2312.

It doesn’t matter what type it is converted into. The key is that the encoding in your PHP WEB application must be consistent.

4. When accessing MySQL programmatically, it is recommended to add a line of code: mysql_query("SET NAMES 'GBK'");

The above is the detailed content of What to do when php reads Chinese garbled characters. 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