What should I do if mysql does not display Chinese when inserting data into php?

藏色散人
Release: 2023-03-11 16:06:01
Original
2195 people have browsed it

The solution to the problem that mysql does not display Chinese when inserting data into php: first find and open mysql.ini; then add the statement "default-character-set = utf8".

What should I do if mysql does not display Chinese when inserting data into php?

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

Why does mysql not display Chinese when inserting data into php manage?

Detailed description of the problem:

When using php to insert Chinese data into the Mysql database, garbled characters are displayed in the database

I wrote The php code inserts a Chinese field value into the mysql database. When viewed in phpMyadmin, the display is garbled. When viewed in the mysql console, it is also garbled. I have changed the sorting rule of each field to uft8_general_ci. When directly inserted in phpmyadmin, the display is normal. In Mysql console direct insertion also shows normal, but it is not normal when inserted through php. I also wrote header("Content-Type: text/html;charset=utf-8");

<?php
header("Content-Type:text/html;charset=utf-8");
if($con=mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;)){
    echo &#39;连接成功&#39;;
}
else{
    echo &#39;连接失败&#39;;
}
if(mysqli_select_db($con,"test")){
    echo &#39;选择数据库成功&#39;;
    }
else{
    echo &#39;选择数据库失败&#39;;
}
$str="insert into students(name,age)values(&#39;周杰伦&#39;,&#39;23&#39;)";
if(mysqli_query($con,$str)){
    echo &#39;插入成功&#39;;
}
else{
    echo &#39;插入失败&#39;;
}
mysqli_close($con);//关闭数据库
?>
Copy after login

in the php code. What should I do if mysql does not display Chinese when inserting data into php?

Solution:

I found the key to the problem. The problem lies in the missing sentence default-character-set = in the mysql.ini configuration. utf8, I installed mysql myself using this software after installing wampserver. His mysql.ini does not have that sentence, and if you add this sentence, you cannot add it randomly. It must be added to the location pointed out in my picture client

What should I do if mysql does not display Chinese when inserting data into php?

Use show variables like 'character_set_%; to view the character encoding after modifying the ini file as shown below

What should I do if mysql does not display Chinese when inserting data into php?

Recommended study: "PHP video tutorial

The above is the detailed content of What should I do if mysql does not display Chinese when inserting data into php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template