Contents
  1. 1. MySQL字符编码问题
    1. 1.1. 缘由
    2. 1.2. 问题分析
      1. 1.2.1. 检查MySQL配置文件my.cnf
      2. 1.2.2. 修改链接层字符

MySQL字符编码问题

缘由

本地开发环境下数据库MySQL中插入中英文插入、读取都ok,无论是MySQL控制台下插入、读取还是借助第三方GUI工具都是ok的,但是部署到线上Linux机器后插入MySQL后,查看中文部分是乱码。

1
2
3
4
#development enviroment
PHP Version 7.0.15
MySQL 5.7.16
Apache 2.0 Handler
1
2
3
4
#online environment
PHP Version 7.0.19
Apache 2.0 + Nginx 1.10
MySQL 5.7.16

问题分析

MySQL相关封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#mysqlhelper.class.php

<?php

/**
*
* Class mysqlHelper
* Author: Lomo
* Email: lomo@lomo.space
* URL: http://lomo.space
* Date: 2017-05
*
*/
include_once 'sql.interface.php';

class mysqlHelper implements ISQLHelper{

private $host;
private $userName;
private $passWord;
//private $defaultDB;

private $conn;

/**
* SqlHelper constructor.
*
* @param $host
* @param $userName
* @param $passWord
*/
public function __construct($host, $userName, $passWord)
{
$this->host = $host;
$this->userName = $userName;
$this->passWord = $passWord;
//$this->dbName = $defaultDB;
$this->conn = new mysqli($this->host, $this->userName, $this->passWord) or die("Fail to Connect MySQL Server".mysqli_error());
}

/**
* @param $db
* switch DataBase
* @return new $conn
*/
public function _selectDB($db) {
mysqli_select_db($this->conn, $db);
}

/**
* @param $sql
* DQL - 执行SQL查询
* @return object $res
*
* $res->num_rows; 返回查询结果集行数
*/
public function _query($sql) {
return mysqli_query($this->conn, $sql);
}

/**
* 判断是否查询有结果, 有则返回TRUE,反之FALSE
* @param $sql
*
* @return bool
*/
public function isExistData($sql) {
$result = $this->_query($sql);
if($result->num_rows >= 1)
{
return true;
}else{
return false;
}
}

/**
* @param $sql
* DQL - 查询并返回关联数组形式的查询结果
* @return array
*/

public function execute_dql2($sql){

$arr = array();
$i = 0;
$res = mysqli_query($this->conn, $sql) or die(mysqli_error($this->conn));
while ($row = mysqli_fetch_assoc($res))
{
$arr[$i++]=$row;
}
mysqli_free_result($res);
return $arr;
}

/**
* @param $sql
* DML - 数据操作
* @return int
* 0 //操作失败
* 1 //操作成功, 数据表受到影响
* 2 //操作成功, 但是未修改任何数据
*/
public function execute_dml($sql){

$r = mysqli_query($this->conn, $sql);
if(!$r){
return 0;
}else{
if(mysqli_affected_rows($this->conn) > 0){
return 1;
}else{
return 2;
}

}

}

/**
* close connect
*/
public function close_connect(){
if(!empty($this->conn)){
mysqli_close($this->conn);
}
}

}

本地run脚本插入读取带有中文的数据一切OK,本地run 链接远程数据库时,插入读取中文相关也ok。当把脚本放在online机器时就产生了乱码问题。

检查MySQL配置文件my.cnf

1
2
3
4
5
6
7
8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server=utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server=utf8

配置文件ok。

修改链接层字符

mysqli_query("set names utf8");
尝试无果。

Bing搜索🔍查询,有一个PHP针对MySQL连接层字符编码设置 mysqli_set_charset($link, ); [PHP官方推荐使用!]

源码定义:

1
2
3
4
5
6
7
8
9
10
# PHP 5 >= 5.0.5, PHP 7

/**
* Sets the default client character set
* @link http://php.net/manual/en/mysqli.set-charset.php
* @param mysqli $link A link identifier returned by mysqli_connect() or mysqli_init()
* @param string $charset
* @return bool
*/
function mysqli_set_charset ($link, $charset) {}

通过在连接层设置字符编码为utf8即可解决。

mysqli_set_charset($this->conn, "utf8");

参考:https://stackoverflow.com/questions/26596294/set-names-vs-mysqli-set-charset-besides-affecting-mysqli-escape-string-are


    
        
        版权声明:
        本文由Lomo创作和发表,采用署名(BY)-非商业性使用(NC)-相同方式共享(SA)国际许可协议进行许可,
        转载请注明作者及出处,本文作者为Lomo,本文标题为messy code in MySQL on Linux by chinese.
    
    


 Leave a message ^_^:

Contents
  1. 1. MySQL字符编码问题
    1. 1.1. 缘由
    2. 1.2. 问题分析
      1. 1.2.1. 检查MySQL配置文件my.cnf
      2. 1.2.2. 修改链接层字符