服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - SQL server不支持utf8 php却用utf8的矛盾问题解决方法

SQL server不支持utf8 php却用utf8的矛盾问题解决方法

2021-10-06 16:19SQL教程网 Sql Server

这篇文章主要介绍了SQL server不支持utf8 php却用utf8的矛盾问题解决方法,需要的朋友可以参考下

核心代码

?
1
2
3
4
5
6
7
8
function convert2utf8($string)
  {
    return iconv("gbk","utf-8",$string);
  }
  function convert2gbk($string)
  {
    return iconv("utf-8","gbk",$string);
  }

当插入数据,或修改数据的时候,把utf-8,转为gbk,存入数据库。

当获取数据的时候,将数据转为utf-8。

这个方法在底层的数据中设计,上层调用即可。

?
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
class dao_dao extends Zend_Db_Table {
 
  public function returnDb(){
    return $db = &$this->getAdapter();
  }
 
  public function getData($table,$where = false, $order = 'id ASC', $pagesize = false, $offset = false, $count = false, $from = false, $join = false, $group = false) {
    $db = &$this->getAdapter();
    $select = $db->select();
    
    if ($where && is_array($where)) {
      foreach ($where as $key => $val) {
        //print_r($where);
        if($val['type']==1){
          $select->where($key, $val['val']);
        }else{
          $select->orwhere($key, $val['val']);
        }
        
      }
    }
 
    if (!$from)
      $from = '*';
    //echo $select."<br/>";
    if ($pagesize) {
      $select->limit($pagesize, $offset);
    }
    //echo $select."<br/>";
    if (is_array($order)) {
      foreach ($order as $value) {
        $select->order($value);
      }
    } else {
      $select->order($order);
    }
    //echo $select."<br/>";
    $select->from($table, $count ? "COUNT(".$table.".id)" : $from);
    
    if (is_array($group)) {
      foreach ($group as $key => $val) {
        $select->group($val);
      }
 
      if ($count) {
        $result = $db->fetchAll($select);
        //echo $select."<br/>";
        return $result;
      }
    } else {
      if ($count) {
        $result = $db->fetchOne($select);
        //echo $select."<br/>";
        return $result;
      }
    }
    if (is_array($join)) {
      foreach ($join as $key => $val) {
        $select->join($key, $val[0], $val[1]);
      }
    }
 
    //echo $select."<br/>";
 
    //echo $select;exit;
    
    $result = $db->fetchAll($select);
    foreach ($result as $key => $value) {
      foreach ($value as $key2 => $value2) {
        $result[$key][$key2] = $this->convert2utf8($value2);
      }
    }
    return $result;
  }
  
  
 
 
  /**
   * 向表中插入数据
   * array $adata 数据
   * string $table 表名
   * int $insterid 是否需要返回插入ID
   * @return true or false or int
   */
   // @bianding 2013.11.04 更改了pdo中mssql.php的lastInsertId()函数
   // @bianding 2013.11.04 经测试 mssql.php中的lastInsertId()函数中的SELECT两种方式都行
  function SaveData($adata, $table, $insterid = 0, $aLog = false) {
    $db = & $this->getAdapter();
    foreach ($adata as $key => $value) {
      $adata[$key] = $this->convert2gbk($value);
    }
    if ($db->insert($table, $adata)) {
      //var_dump($db->getProfiler());
      $insertedID = $db->lastInsertId();     
      if ($insterid) {
        return $insertedID;
      } else {
        return TRUE;
      }
    } else {
      return false;
    }
  }
 
  /**
   * 删除表中数据
   *
   * @param string $table 表名
   * @param string $where 'id ='.$id 条件
   * @return true or false
   */
  function DelData($table, $where, $aLog = false) {
    $db = & $this->getAdapter();
    if ($db->delete($table, $where)) {
      return TRUE;
    } else {
      return FALSE;
    }
  }
 
  /**
   * 更新表中数据
   *
   * @param string $table
   * @param array $adata
   * @param string $where 'id ='.$id
   * @return true or false
   */
  function UpdateData($table, $adata, $cond, $aLog = false) {
    $db = & $this->getAdapter();
    foreach ($adata as $key => $value) {
      $adata[$key] = $this->convert2gbk($value);
    }
    if ($db->update($table, $adata, $cond)) {
      return TRUE;
    } else {
      return false;
    }
  }
 
 
  public function clearTable($table) {
    $db = &$this->getAdapter();
    $result = $db->query('TRUNCATE TABLE ' . $table);
  }
 
  public function executeSql($strSql) {
    $db = &$this->getAdapter();
    $result = $db->query($strSql);
  }
  
  function convert2utf8($string)
  {
    return iconv("gbk","utf-8",$string);
  }
  function convert2gbk($string)
  {
    return iconv("utf-8","gbk",$string);
  }
}

sqlserver 建库指定utf-8 修改库为utf-8编码

CREATE DATABASE paas COLLATE Chinese_PRC_CI_AS
GO
ALTER DATABASE paas COLLATE Chinese_PRC_CI_AS
GO

让ASP和MS SQL SERVER支持UTF-8编码存储多国语言文字

近日在ASP+MS SQL存储UTF-8编码内容的时候,出现乱码的情况,经过查询发现要使SQL SERVER支持UTF-8编码格式,必须做一些修改才可以。

1、确保ASP页面是UTF-8编码的,并在ASP页面顶部声明中使用<%@ LANGUAGE = VBScript CodePage = 65001%>进行编码声明

2、输出的HTML页面中声明字符集:<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

3、在进行URL参数传递的时候,要使用Server.URLEncode()方法进行编码

4、使用JS进行URL参数传递中文的时候,要使用escape进行编码

5、在将UTF-8编码的内容存入SQL SERVER数据库中的时候,要存储的字段必须设置为NVARCHAR类型,SQL语句要在内容前加N表示,如insert into user (name) values (N´&username&´),除id意外的字段都需要加N。

延伸 · 阅读

精彩推荐