Home > PHP Framework > ThinkPHP > How to solve the problem of garbled Chinese characters in thinkphp

How to solve the problem of garbled Chinese characters in thinkphp

藏色散人
Release: 2021-12-21 11:43:22
Original
2798 people have browsed it

Solution to garbled Chinese characters in thinkphp: 1. Find and open the entry file of thinkphp; 2. Add "header('Content-Type: text/html; charset=utf-8'); to the file; "That's it.

How to solve the problem of garbled Chinese characters in thinkphp

#The operating environment of this article: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

How to solve the problem of garbled Chinese characters in thinkphp?

The problem of Chinese utf-8 garbled output from thinkphp database

The problem of Chinese garbled characters occurs when using thinkphp to connect to the mysql database It's really a headache. No matter how I modify the field type of the database, it doesn't work. I also checked everywhere and found that it is not a problem with the mysql database. When we configured the mysql database, we had already configured the database as utf8. format, which means that the problem lies in the thinkphp configuration itself. I checked it online and verified it. In fact, it is very simple, just add a sentence to the thinkphp entry file:

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

In this way we Then call the database for verification, and the garbled characters become Chinese successfully.

Generally speaking, the reason for Chinese garbled characters is that the character encoding is not unified, but what gives many people a headache is that they don’t know where the encoding needs to be changed and how to unify it.

There are places where you need to agree on the encoding, and how to unify it:

1. Code file (the easiest way to determine the encoding of the file is: open it with Notepad, Then click "Save As", and there will be the word "Encoding" at the bottom of the window that appears. If it is not the encoding you want, change it to your encoding and save it and overwrite the original file);

2. html file The header should be added with and change utf-8 to the encoding you want to agree with;

3. The database connection code should select the corresponding character encoding. The code for selecting the character encoding for mySql is: mysql_query("SET NAMES 'utf-8';"); Please query the codes of other databases by yourself;

4. The background program code php needs to indicate the character encoding, header('Content-Type: text/html; charset=utf-8');

Recommended study: "The latest 10 thinkphp video tutorial

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

Related labels:
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