php连接MySQL数据库和控制台MySQL数据库中文乱码问题?

1.环境是:https://img2.mukewang.com/5c8f6a90000178b607370277.jpg

是用的xampp自带的数据库配置的。
字符集信息:

MariaDB [testdb2]> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8                           |
| character_set_connection | utf8                           |
| character_set_database   | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | utf8                           |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | X:\xampp\mysql\share\charsets\ |
+--------------------------+--------------------------------+

2.问题出现在:
操作1:用php插入数据(包含中文),并在浏览器中显示查询结果。
操作2:用控制台插入数据(包含中文),并在控制台显示查询结果。

Case1:
因为操作台是gbk编码,所以如果set names gbk; 并且数据库、数据表都设置gbk,那么操作2正常,但用php查询并将查询结果返回在浏览器中就乱码。
控制台中代码如下:

MariaDB [testdb2]> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | gbk                            |
| character_set_connection | gbk                            |
| character_set_database   | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | gbk                            |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | X:\xampp\mysql\share\charsets\ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)

MariaDB [testdb2]> select * from ceshibiao
    -> ;
Empty set (0.00 sec)

MariaDB [testdb2]> insert into ceshibiao(hanzi,pinyin) values("哈哈哈","hhh");
Query OK, 1 row affected (0.08 sec)

MariaDB [testdb2]> select * from ceshibiao;
+--------+--------+
| hanzi  | pinyin |
+--------+--------+
| 哈哈哈 | hhh    |
+--------+--------+
1 row in set (0.01 sec)

PHP代码如下:

<?php
    header("Content-Type:text/html;charset=utf-8");
    $dbhost = 'localhost:3306';
    $dbuser = 'zzjzxh';
    $dbpass = '*******';
    $dbname = 'testdb2';
    $mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
    if ($mysqli->connect_error)
    {
        echo "database error";
        echo "<br>";
    }
    else
    {
        echo "php env successfully";
        echo "<br>";
    }
    $sql = "SELECT hanzi,pinyin FROM ceshibiao;";
    $result = $mysqli->query($sql)->fetch_array();
    print($sql);
    print("----");
    print_r($result);
    print("<br>");
?>

结果:
https://img.mukewang.com/5c8f6a9100017b5008000104.jpg

//我的想法是:在存储过程中是用的gbk编码,但在显示时使用utf-8编码,所以导致解码时错误出现乱码。

Case2:
在控制台输入chcp 65001
控制台也使用utf-8编码
https://img1.mukewang.com/5c8f6a920001416204550594.jpg

控制台查看数据库字符集信息:

MariaDB [testdb2]> show variables like 'character%';
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8                           |
| character_set_connection | utf8                           |
| character_set_database   | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | utf8                           |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | X:\xampp\mysql\share\charsets\ |
+--------------------------+--------------------------------+
8 rows in set (0.00 sec)

MariaDB [testdb2]> select * from ceshibiao;
Empty set (0.00 sec)

PHP进行插入和查询:

<?php
    header("Content-Type:text/html;charset=utf-8");

    $dbhost = 'localhost:3306';
    $dbuser = 'zzjzxh';
    $dbpass = '*******';
    $dbname = 'testdb2';
    $mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
    if ($mysqli->connect_error)
    {
        echo "database error";
        echo "<br>";
    }
    else
    {
        echo "php env successfully";
        echo "<br>";
    }
    
    $mysqli->query("set names utf8;");
    $check = $mysqli->query("insert into ceshibiao(hanzi,pinyin) values('啊啊','aa');");
    if (!$check) print("插入失败!<br>");

    $sql = "SELECT hanzi,pinyin FROM ceshibiao;";
    $result = $mysqli->query($sql)->fetch_array();
    print($sql);
    print("----");
    print_r($result);
    print("<br>");
?>

结果:
https://img.mukewang.com/5c8f6a93000129b908000099.jpg
是可以正常显示中文的。在控制台中:

MariaDB [testdb2]> select * from ceshibiao;
+--------+--------+
| hanzi  | pinyin |
+--------+--------+
| 啊啊   | aa     |
+--------+--------+
1 row in set (0.00 sec)

也是可以查询的。
但是在控制台中插入中文就会出错:

MariaDB [testdb2]> truncate table ceshibiao;
Query OK, 0 rows affected (0.33 sec)

MariaDB [testdb2]> insert into ceshibiao(hanzi,pinyin) values("搜得死","sds");
Query OK, 1 row affected (0.09 sec)

MariaDB [testdb2]> select * from ceshibiao;
+-------+--------+
| hanzi | pinyin |
+-------+--------+
| ???   | sds    |
+-------+--------+
1 row in set (0.00 sec)

再用PHP查询结果:
https://img3.mukewang.com/5c8f6a940001c73908000102.jpg
同样乱码。

3.我的需求:
实现PHP对数据库插入、查询和控制台对数据库插入、查询都能够正常显示中文。

一只甜甜圈
浏览 608回答 7
7回答

茅侃侃

所有都设置uft-8就可以了,包括php连接数据库也最好运行一次类似set names utf8的命令。其实用多了就发现,最好别用windows的cmd界面处理问题,可以考虑用navicat或者phpadmin来管理数据。后期甚至最好在虚拟机linux里面架服务器,基本没有编码问题。

叮当猫咪

php这边设置的utf-8是没问题的,在终端这边你输入中文是什么编码的问题。可以在终端这边修改掉。

www说

我猜你是少了这步 // 修改数据库连接字符集为 utf8 mysqli_set_charset($con,"utf8"); mysqli_query($db,"SET NAMES utf8");

白板的微信

链接时 字符集是否设置成 utf8 mysqli_set_charset($connection,"utf8");

慕码人2483693

mysql client中执行:set names utf8;

凤凰求蛊

我不懂为什么你要分开来操作.....程序能通就好了.

吃鸡游戏

连接数据库修改字符集为utf8
打开App,查看更多内容
随时随地看视频慕课网APP