Home  >  Article  >  Backend Development  >  What should I do if php can write English but not Chinese?

What should I do if php can write English but not Chinese?

藏色散人
藏色散人Original
2023-01-18 09:48:342191browse

The solution to the problem that php can write English but not Chinese: 1. Add "header("Content-type=text/html;charset=utf-8");" to the PHP page itself; 2. . After establishing the database connection, change the encoding method of the connection to Chinese; 3. Set "mysql_query("set names utf8");".

What should I do if php can write English but not Chinese?

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

php can be written in English but not What should I do if I enter Chinese?

Solution:

1. The php page itself

header("Content-type=text/html;charset=utf-8");

2. After establishing the database connection, encode the connection Changed 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);//将连接改为中文编码方式。这种方式 仅能保证插入能正常执行,但是插入之后的数据时???乱码。有待继续解决。

Continue with the above question.

I first checked the PHP source files in the Apache directory and found that the encoding method of the page was ANSI, so I changed it to utf8 format.

Then add this sentence before executing the SQL statement command,

mysql_query("set names utf8");// 这句话的意思是:使php写入mysql的编码为utf-8
可以防止phpmyadmin中查看mysql的中文数据出现乱码!//很重要!!

Then perform the insert operation again, and the Chinese can be inserted into the database normally, no longer? ? ? The format is garbled. .

To summarize, 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 this If the three points are consistent, you can write Chinese successfully! !

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What should I do if php can write English but not Chinese?. 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