服务器之家

服务器之家 > 正文

php实现的mysqldb读写分离操作类示例

时间:2021-04-20 15:55     来源/作者:风起从容

本文实例讲述了php实现的mysqldb读写分离操作类。分享给大家供大家参考,具体如下:

?
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* php MysqlDB 读写分离类
* -----------------------------------------------------
* $Source: http://code.ilaopo.net/php.class.mysqldb $
* $Author: Bevin Chen $
* $Email: bevin#lifa8.cn $
* $Date: 2009-10-10 $
* -----------------------------------------------------
*/
class mysqldb {
  var $querynum = 0;
  var $linkr,$linkw,$charset,$pconnect,$dbconfig;
  function __constructor($dbarray) {
    $this->mysqldb($dbarray);
  }
  function mysqldb($dbarray,$dbcharset='utf8',$pcontent=0) {
    if(!is_array($dbarray[0])) {
      echo "数据库参数错误";
      return false;
    }
    $this->charset = $dbcharset;
    $this->pconnect = $pconnect;
    $this->dbconfig['master'] = $dbarray[0];
    $dbServerNum = count($dbarray);
    if($dbServerNum > 1) {
      /*
  * 当x=1 时,$dbarray[0]不作slave
  * 当x=0 时,$dbarray[0]作slave
  */
      $x = 1;
      $rand = rand($x,$dbServerNum-1);
      $this->dbconfig['slave'] = $dbarray[$rand];
    } else {
      $this->dbconfig['slave'] = false;
    }
  }
  function connect($dbhost,$dbuser,$dbpw,$dbname) {
    if($this->pconnect) {
      $link = @mysql_pconnect($dbhost, $dbuser, $dbpw);
    } else {
      $link = @mysql_connect($dbhost, $dbuser, $dbpw, 1);
    }
    if($link) {
      if($this->version($link) > '4.1') {
        if($this->charset) {
          @mysql_query("SET character_set_connection=".$this->charset.", character_set_results=".$this->charset.", character_set_client=binary", $link);
        }
        if($this->version($link) > '5.0.1') {
          @mysql_query("SET sql_mode=''", $link);
        }
      }
      if($dbname) {
        @mysql_select_db($dbname, $link);
      }
      //print_r($link);
      return $link;
    } else {
      return false;
    }
  }
  function connectM() {
    if(!$this->linkw = $this->connect($this->dbconfig['master'][0],$this->dbconfig['master'][1],$this->dbconfig['master'][2],$this->dbconfig['master'][3])) {
      exit("主数据库连接失败!");
    }
    //echo "<br>##connectM!.<br>";
  }
  function connectS() {
    if($this->dbconfig['slave']) {
      if(!$this->linkr = $this->connect($this->dbconfig['slave'][0],$this->dbconfig['slave'][1],$this->dbconfig['slave'][2],$this->dbconfig['slave'][3])) {
        $this->dbconfig['slave'] = false;
        $this->connectS();
      }
      //echo "<br>##connectS!.<br>";
    } else {
      if(!$this->linkw) {
        $this->connectM();
      }
      $this->linkr = $this->linkw;
    }
  }
  // db read
  function query($sql, $type = '') {
    if(!$this->linkr) {
      $this->connectS();
    }
    $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
      'mysql_unbuffered_query' : 'mysql_query';
    $query = $func($sql, $this->linkr);
    $this->querynum++;
    return $query;
  }
  function fetch_array($query, $result_type = MYSQL_ASSOC) {
    return @mysql_fetch_array($query, $result_type);
  }
  function fetch_row($query) {
    $query = mysql_fetch_row($query);
    return $query;
  }
  function fetch_One($sql) {
    $query = $this->query($sql);
    return $this->fetch_row($query);
  }
  /* 获取分页方法 */
  function fetch_page($sql, $pagenum) {
    $page = intval($_GET['page']);
    $query = $this->query($sql);
    $countnum = $this->num_rows($query);
    $countpage = ceil($countnum/$pagenum);
    if($page<1) {
      $page=1;
    }
    if($page>$countpage) {
      $page=$countpage;
    }
    $limitstart = ($page-1)*$pagenum;
    /* 获取数据结果集 */
    for ($i=0; $i<($limitstart+$pagenum); $i++) {
      if($i>=$countnum) {
        break;
      }
      if($i>=$limitstart) {
        $result[] = $this->fetch_array($query);
      } else {
        $this->fetch_array($query);
      }
    }
    /* 生成url */
    $url = $_SERVER['QUERY_STRING'];
    $url = preg_replace("/&?page=[0-9]+/i","",$url);
    $array['countnum'] = $countnum;
    $array['countpage'] = $countpage;
    $array['result'] = $result;
    $array['page'] = $page;
    if($page>1) {
      $array['preurl'] = "?".$url."&page=".($page-1);
      $array['prepage'] = $page-1;
    } else {
      $array['preurl'] = "?".$url."&page=1";
      $array['prepage'] = 1;
    }
    if($page<$countpage) {
      $array['nexturl'] = "?".$url."&page=".($page+1);
      $array['nextpage'] = $page+1;
    } else {
      $array['nexturl'] = "?".$url."&page=".$countpage;
      $array['nextpage'] = $countpage;
    }
    $array['firsturl'] = "?".$url."&page=1";
    $array['firstpage'] = 1;
    $array['lasturl'] = "?".$url."&page=".$countpage;
    $array['lastpage'] = $countpage;
    $array['nopage'] = "?".$url."&page=";
    return $array;
  }
  // db write
  function queryw($sql, $type = '') {
    if(!$this->linkw) {
      $this->connectM();
    }
    $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
      'mysql_unbuffered_query' : 'mysql_query';
    $query = $func($sql, $this->linkw);
    $this->querynum++;
    return $query;
  }
  function update($table,$where,$array) {
    $whereset = $set = $dot = '';
    foreach($array as $key=>$value) {
      $set .= $dot." `$key`='$value'";
      $dot = ',';
    }
    $whereset = $dot = '';
    foreach($where as $k=>$v) {
      $whereset .= $dot." `$k`='$v'";
      $dot = ' and ';
    }
    $sql = "update $table set $set where $whereset";
    return $this->queryw($sql);
  }
  function insert($table,$array) {
    $col = $v = $dot = '';
    foreach($array as $key=>$value) {
      $v .= $dot."'$value'";
      $col .= $dot."`$key`";
      $dot = ',';
    }
    $sql = "insert into $table ($col) values ($v)";
    if($this->queryw($sql)) {
      return $this->insert_id();
    } else {
      return false;
    }
  }
  function insert_id() {
    return ($id = mysql_insert_id($this->linkw)) >= 0 ? $id : $this->result($this->queryw("SELECT last_insert_id()"), 0);
  }
  // db other
  function affected_rows($link) {
    return mysql_affected_rows($link);
  }
  function error($link) {
    return (($link) ? mysql_error($link) : mysql_error());
  }
  function errno($link) {
    return intval(($link) ? mysql_errno($link) : mysql_errno());
  }
  function result($query, $row) {
    $query = @mysql_result($query, $row);
    return $query;
  }
  function num_rows($query) {
    $query = mysql_num_rows($query);
    return $query;
  }
  function num_fields($query) {
    return mysql_num_fields($query);
  }
  function free_result($query) {
    return mysql_free_result($query);
  }
  function fetch_fields($query) {
    return mysql_fetch_field($query);
  }
  function version($link) {
    return mysql_get_server_info($link);
  }
  function close($link) {
    return mysql_close($link);
  }
}
/* 测试例子 //
* $dbarray[] = array('localhost','bevin','password','test');
* $dbarray[] = array('ilaopo.net','root','password','test');
* $dbarray[] = array('192.168.1.77','cxh','password','test');
* $newdb = new mysqldb($dbarray,'utf8');
* $array = array('name' => date("Y-m-d H:i:s"));
* $id = $newdb->insert("test",$array);
* $result = $newdb->fetch_page("select * from test order by id desc",'10');
* print_r($result);
* $id = $newdb->insert("test",$array);
* echo $id;
*/

希望本文所述对大家PHP程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部