SoFunction
Updated on 2025-03-09

Solution to garbled code in php reading mysql Chinese data

The encoding type of the page language itself is not suitable. At this time, the Chinese you write directly in the script must be garbled, not to mention the database

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

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

Solution:When creating a database, select 'UTF8' for MySQL character set, and select utf8_general_ci for MySQL connection proofreading. There is definitely no problem with the database created in this way to store Chinese.
Otherwise, your Chinese is garbled in MySQL first, and don't expect it to show you correctly in the PHP page.

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

Solution:Try to use the same editor. If you copy 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 you convert it to. The key is to have the same encoding in your PHP WEB application.

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