Home > Article > Backend Development > What to do if Chinese characters are garbled in php mysql
php The solution to the garbled Chinese characters in mysql: first check the PHP source file in the Apache directory and change it to utf8 format; then add the code "mysql_query("set names utf8" before executing the SQL statement command );" That's it.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
An example solution to the Chinese garbled code when writing mysql in php
The solution to the Chinese garbled code in php writing to mysql is: establishing a database connection Afterwards, change the encoding of the connection to Chinese.
The code is as follows:
$linkID=@mysql_connect("localhost","root","admin"); if(!$linkID) { echo "数据库连接失败!"; } echo "数据库连接成功!"; mysql_query("SET character_set_connection = GBK",$linkID);//将连接改为中文编码方式。这种方式
[Recommended learning: "PHP Video Tutorial"]
can only guarantee that the insertion can be normal Executed, but when inserting subsequent data? ? ? Garbled characters. To be further resolved.
Continuing with the above question, I checked the PHP source file in the Apache directory and found that the encoding method of the page was ANSI, so I changed it to utf8 format. Then add the following code before executing the SQL statement command:
mysql_query("set names utf8");
This sentence means: Make the encoding of php written to mysql as utf-8
This can prevent mysql from being viewed in phpmyadmin Chinese data is garbled! //Very important! ! Then perform the insert operation again, and the Chinese can be inserted into the database normally, no longer? ? ? The format is garbled.
Summary: If you write Chinese into the Mysql database through PHP, you must ensure that the encoding format of the PHP source file is utf8, the mysql connection must maintain the utf8 format, and the encoding format of the database table is utf8. Ensure these three If the points are consistent, Chinese can be written successfully! !
The above is the detailed content of What to do if Chinese characters are garbled in php mysql. For more information, please follow other related articles on the PHP Chinese website!