Home >Backend Development >PHP Tutorial >When using php to insert Chinese data into the Mysql database, the database displays garbled characters

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

WBOY
WBOYOriginal
2016-09-12 17:44:411265browse

1. I wrote php code to insert a field value with Chinese characters into the mysql database. Viewing it in phpMyadmin shows garbled characters, and viewing it in the mysql console is also garbled. I have changed the sorting rule of each field to uft8_general_ci in phpmyadmin. Direct insertion shows normal, direct insertion in mysql console also shows normal, but inserting through php is not normal. I also wrote header("Content-Type: text/html;charset=utf-8");

in php code.
<code><?php
header("Content-Type:text/html;charset=utf-8");
if($con=mysqli_connect('localhost','root','')){
    echo '连接成功';

}
else{
    echo '连接失败';
}
if(mysqli_select_db($con,"test")){
    echo '选择数据库成功';
    }
else{
    echo '选择数据库失败';
}

$str="insert into students(name,age)values('周杰伦','23')";

if(mysqli_query($con,$str)){
    echo '插入成功';
}
else{
    echo '插入失败';
}
mysqli_close($con);//关闭数据库
?></code>

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

3. Could someone please help me figure out where the problem is?

Reply content:

1. I wrote php code to insert a field value with Chinese characters into the mysql database. Viewing it in phpMyadmin shows garbled characters, and viewing it in the mysql console is also garbled. I have changed the sorting rule of each field to uft8_general_ci in phpmyadmin. Direct insertion shows normal, direct insertion in mysql console also shows normal, but inserting through php is not normal. I also wrote header("Content-Type: text/html;charset=utf-8");

in php code.
<code><?php
header("Content-Type:text/html;charset=utf-8");
if($con=mysqli_connect('localhost','root','')){
    echo '连接成功';

}
else{
    echo '连接失败';
}
if(mysqli_select_db($con,"test")){
    echo '选择数据库成功';
    }
else{
    echo '选择数据库失败';
}

$str="insert into students(name,age)values('周杰伦','23')";

if(mysqli_query($con,$str)){
    echo '插入成功';
}
else{
    echo '插入失败';
}
mysqli_close($con);//关闭数据库
?></code>

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

3. Could someone please help me figure out where the problem is?

See if your database table contains normal Chinese characters, then try to solve the problem when inserting data. Unicode format:

  • Make sure the database, table, and field settings are all utf-8

  • Make sure the character set when connecting to the database is utf-8

  • Make sure the program file is utf-8

  • Make sure the configuration file of the mysql installer is set to characters utf-8

I found the key to the problem. I tried both of the two answers below, but it didn’t work. The problem was that the mysql.ini configuration was missing a sentence of default-character-set = utf8. I installed wampserver and used this software myself. The mysql.ini of the installed mysql does not have that sentence, and if you add this sentence, you cannot add it randomly. It must be added at the location pointed out in my pictureWhen using php to insert Chinese data into the Mysql database, the database displays garbled characters

When using php to insert Chinese data into the Mysql database, the database displays garbled characters

Use show variables like 'character_set_%; to view the character encoding after modifying the ini file as shown below
When using php to insert Chinese data into the Mysql database, the database displays garbled characters

You need to set mysql encoding and execute sql "set names utf8"

Try setting the character set

<code>mysqli_set_charset($con, "utf8");</code>
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