Home >Backend Development >PHP Problem >What should I do if php cannot write Chinese data into the database table?
Solution to the problem that php cannot write Chinese data into the database table: 1. Change the character set used by mysql to utf8; 2. Change the character set used for php file encoding to utf-8; 3. Change the front page The character encoding used is changed to utf-8.
The operating environment of this article: windows10 system, php 7&&mysql 5.0, thinkpad t480 computer.
In the process of learning PHP, we may encounter situations where Chinese data cannot be inserted into the data table, and the front-end web page cannot correctly display the Chinese data obtained from the database. When encountering this problem, we need to change the character set used by mysql to utf8, change the character set used for PHP file encoding to utf-8, and change the character encoding used by the front-end web page to utf-8, so that the problem can be solved.
The specific steps are as follows:
1. Run MySQLInstanceConfig.exe in the MySQL installation directory, and configure the default encoding to utf8;
Remember to select "Best Support For Multilingualism", and at the same time Select "utf8" from the drop-down list below
After the configuration is completed, check the setting of "default-character-set" in the my.ini file under the MySQL installation path. It should be set to utf8 at this time. , if not, set to default-character-set=utf8
2.1 Specify utf8 encoding when creating the database:
CREATE DATABASE database_name CHARACTER SET “utf8” COLLATE “utf8_general_ci”;
2.2 Specify utf8 encoding when creating the data table:
CREATE TABLE table_name ( Column_name datatype, ...... )ENGINE=InnoDB DEFAULT CHARSET=utf8;
3. Set the file encoding to utf-8;
If you are using PhpStorm, the default file encoding is utf-8, no need to change it
4. Set the page to use The character set is utf-8:
5. Before operating on the data, you should first perform the mysql_query('set names utf8'); operation, and then perform query, update, insertion, etc. on the data.
6. Finally, set the character set used by the client source data and the query result character set to gbk:
SET character_set_client =gbk; //Insert Chinese directly from the MySQL command line client When storing data, the data will not be garbled
SET character_set_results =gbk; //The Chinese data returned from the MySQL command line client will not be garbled
PS: The above content is for MySQL Server 5.0, in MySQL Server 5.6 the default character encoding used is utf8
Recommended learning: php training
The above is the detailed content of What should I do if php cannot write Chinese data into the database table?. For more information, please follow other related articles on the PHP Chinese website!