Home  >  Article  >  Backend Development  >  How to solve the problem of garbled data inserted in php

How to solve the problem of garbled data inserted in php

藏色散人
藏色散人Original
2020-11-20 09:29:471999browse

Solution to php inserting garbled data: First, set the character set of the data table to utf8; then modify the character set format; then create a database with a character set of utf-8; and finally insert data through the php mysql statement. Can.

How to solve the problem of garbled data inserted in php

# Recommended: "

PHP Video Tutorial

Solution to the problem of garbled characters in mysql database

What we are most afraid of when using the database (mysql) is that the Chinese characters in the database are garbled, and Baidu in addition to changing the characters in the configuration file There doesn't seem to be any other constructive methods. What's more important is that I have tried all these methods one by one, and they don't seem to solve the problem. Then I will provide you with a more practical method that I have been using.

Because I am learning PHP development, the methods I mentioned can all be used in PHP code. I have not tried it in other programming languages, but the commands to operate the MySQL database are all the same, so it should It can also be used. I will talk about the method I mentioned below.

  • First of all, we need to set the character set of the data table to utf8. If the data table is created using the phpmyadmin graphical interface, find the sorting option directly in the table creation interface, as shown below:


    How to solve the problem of garbled data inserted in php

    Then select the character set format as shown below:

How to solve the problem of garbled data inserted in php

If you create a data table through commands in the command window , we can use the create database database name character set utf8; command to create a database with a character set of utf-8. The character set of the data table created under this database is utf8, otherwise it is latin1. Example:

Use create database database name; command to create database A and create data table first


How to solve the problem of garbled data inserted in php By show create table data table name; viewing the table structure we can see that its character set is latin1

Use the create database database name character set utf8; command to create database B and create the data table first


How to solve the problem of garbled data inserted in php Looking at the table structure, you can see that the character set of the table is utf8

    Secondly, just changing the character set of the database cannot achieve the goal. Corresponding measures must be taken after connecting to the database in the code, otherwise the Chinese will still be saved in the database in the form of garbled characters. For example, in PHP, we can avoid the problem of inserting garbled characters by performing the following operations after connecting to the database:

  • How to solve the problem of garbled data inserted in php
We can avoid the problem of Chinese garbled characters by performing the above two operations during the programming process. , be sure to note that these two parts of the operation are indispensable.

The above is the detailed content of How to solve the problem of garbled data inserted in php. 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