Home > Article > PHP Framework > How to solve the problem of garbled Chinese characters in thinkphp
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.
#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');
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 97f2b6ae5f4f65bc7d15c6c9b9e8be8c 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!