当前位置:Gxlcms > PHP教程 > php实现的mysqldb读写分离操作类示例

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

时间:2021-07-01 10:21:17 帮助过:44人阅读

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

  1. /**
  2. * php MysqlDB 读写分离类
  3. * -----------------------------------------------------
  4. * $Source: http://code.ilaopo.net/php.class.mysqldb $
  5. * $Author: Bevin Chen $
  6. * $Email: bevin#lifa8.cn $
  7. * $Date: 2009-10-10 $
  8. * -----------------------------------------------------
  9. */
  10. class mysqldb {
  11. var $querynum = 0;
  12. var $linkr,$linkw,$charset,$pconnect,$dbconfig;
  13. function __constructor($dbarray) {
  14. $this->mysqldb($dbarray);
  15. }
  16. function mysqldb($dbarray,$dbcharset='utf8',$pcontent=0) {
  17. if(!is_array($dbarray[0])) {
  18. echo "数据库参数错误";
  19. return false;
  20. }
  21. $this->charset = $dbcharset;
  22. $this->pconnect = $pconnect;
  23. $this->dbconfig['master'] = $dbarray[0];
  24. $dbServerNum = count($dbarray);
  25. if($dbServerNum > 1) {
  26. /*
  27. * 当x=1 时,$dbarray[0]不作slave
  28. * 当x=0 时,$dbarray[0]作slave
  29. */
  30. $x = 1;
  31. $rand = rand($x,$dbServerNum-1);
  32. $this->dbconfig['slave'] = $dbarray[$rand];
  33. } else {
  34. $this->dbconfig['slave'] = false;
  35. }
  36. }
  37. function connect($dbhost,$dbuser,$dbpw,$dbname) {
  38. if($this->pconnect) {
  39. $link = @mysql_pconnect($dbhost, $dbuser, $dbpw);
  40. } else {
  41. $link = @mysql_connect($dbhost, $dbuser, $dbpw, 1);
  42. }
  43. if($link) {
  44. if($this->version($link) > '4.1') {
  45. if($this->charset) {
  46. @mysql_query("SET character_set_connection=".$this->charset.", character_set_results=".$this->charset.", character_set_client=binary", $link);
  47. }
  48. if($this->version($link) > '5.0.1') {
  49. @mysql_query("SET sql_mode=''", $link);
  50. }
  51. }
  52. if($dbname) {
  53. @mysql_select_db($dbname, $link);
  54. }
  55. //print_r($link);
  56. return $link;
  57. } else {
  58. return false;
  59. }
  60. }
  61. function connectM() {
  62. if(!$this->linkw = $this->connect($this->dbconfig['master'][0],$this->dbconfig['master'][1],$this->dbconfig['master'][2],$this->dbconfig['master'][3])) {
  63. exit("主数据库连接失败!");
  64. }
  65. //echo "<br>##connectM!.<br>";
  66. }
  67. function connectS() {
  68. if($this->dbconfig['slave']) {
  69. if(!$this->linkr = $this->connect($this->dbconfig['slave'][0],$this->dbconfig['slave'][1],$this->dbconfig['slave'][2],$this->dbconfig['slave'][3])) {
  70. $this->dbconfig['slave'] = false;
  71. $this->connectS();
  72. }
  73. //echo "<br>##connectS!.<br>";
  74. } else {
  75. if(!$this->linkw) {
  76. $this->connectM();
  77. }
  78. $this->linkr = $this->linkw;
  79. }
  80. }
  81. // db read
  82. function query($sql, $type = '') {
  83. if(!$this->linkr) {
  84. $this->connectS();
  85. }
  86. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  87. 'mysql_unbuffered_query' : 'mysql_query';
  88. $query = $func($sql, $this->linkr);
  89. $this->querynum++;
  90. return $query;
  91. }
  92. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  93. return @mysql_fetch_array($query, $result_type);
  94. }
  95. function fetch_row($query) {
  96. $query = mysql_fetch_row($query);
  97. return $query;
  98. }
  99. function fetch_One($sql) {
  100. $query = $this->query($sql);
  101. return $this->fetch_row($query);
  102. }
  103. /* 获取分页方法 */
  104. function fetch_page($sql, $pagenum) {
  105. $page = intval($_GET['page']);
  106. $query = $this->query($sql);
  107. $countnum = $this->num_rows($query);
  108. $countpage = ceil($countnum/$pagenum);
  109. if($page<1) {
  110. $page=1;
  111. }
  112. if($page>$countpage) {
  113. $page=$countpage;
  114. }
  115. $limitstart = ($page-1)*$pagenum;
  116. /* 获取数据结果集 */
  117. for ($i=0; $i<($limitstart+$pagenum); $i++) {
  118. if($i>=$countnum) {
  119. break;
  120. }
  121. if($i>=$limitstart) {
  122. $result[] = $this->fetch_array($query);
  123. } else {
  124. $this->fetch_array($query);
  125. }
  126. }
  127. /* 生成url */
  128. $url = $_SERVER['QUERY_STRING'];
  129. $url = preg_replace("/&?page=[0-9]+/i","",$url);
  130. $array['countnum'] = $countnum;
  131. $array['countpage'] = $countpage;
  132. $array['result'] = $result;
  133. $array['page'] = $page;
  134. if($page>1) {
  135. $array['preurl'] = "?".$url."&page=".($page-1);
  136. $array['prepage'] = $page-1;
  137. } else {
  138. $array['preurl'] = "?".$url."&page=1";
  139. $array['prepage'] = 1;
  140. }
  141. if($page<$countpage) {
  142. $array['nexturl'] = "?".$url."&page=".($page+1);
  143. $array['nextpage'] = $page+1;
  144. } else {
  145. $array['nexturl'] = "?".$url."&page=".$countpage;
  146. $array['nextpage'] = $countpage;
  147. }
  148. $array['firsturl'] = "?".$url."&page=1";
  149. $array['firstpage'] = 1;
  150. $array['lasturl'] = "?".$url."&page=".$countpage;
  151. $array['lastpage'] = $countpage;
  152. $array['nopage'] = "?".$url."&page=";
  153. return $array;
  154. }
  155. // db write
  156. function queryw($sql, $type = '') {
  157. if(!$this->linkw) {
  158. $this->connectM();
  159. }
  160. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  161. 'mysql_unbuffered_query' : 'mysql_query';
  162. $query = $func($sql, $this->linkw);
  163. $this->querynum++;
  164. return $query;
  165. }
  166. function update($table,$where,$array) {
  167. $whereset = $set = $dot = '';
  168. foreach($array as $key=>$value) {
  169. $set .= $dot." `$key`='$value'";
  170. $dot = ',';
  171. }
  172. $whereset = $dot = '';
  173. foreach($where as $k=>$v) {
  174. $whereset .= $dot." `$k`='$v'";
  175. $dot = ' and ';
  176. }
  177. $sql = "update $table set $set where $whereset";
  178. return $this->queryw($sql);
  179. }
  180. function insert($table,$array) {
  181. $col = $v = $dot = '';
  182. foreach($array as $key=>$value) {
  183. $v .= $dot."'$value'";
  184. $col .= $dot."`$key`";
  185. $dot = ',';
  186. }
  187. $sql = "insert into $table ($col) values ($v)";
  188. if($this->queryw($sql)) {
  189. return $this->insert_id();
  190. } else {
  191. return false;
  192. }
  193. }
  194. function insert_id() {
  195. return ($id = mysql_insert_id($this->linkw)) >= 0 ? $id : $this->result($this->queryw("SELECT last_insert_id()"), 0);
  196. }
  197. // db other
  198. function affected_rows($link) {
  199. return mysql_affected_rows($link);
  200. }
  201. function error($link) {
  202. return (($link) ? mysql_error($link) : mysql_error());
  203. }
  204. function errno($link) {
  205. return intval(($link) ? mysql_errno($link) : mysql_errno());
  206. }
  207. function result($query, $row) {
  208. $query = @mysql_result($query, $row);
  209. return $query;
  210. }
  211. function num_rows($query) {
  212. $query = mysql_num_rows($query);
  213. return $query;
  214. }
  215. function num_fields($query) {
  216. return mysql_num_fields($query);
  217. }
  218. function free_result($query) {
  219. return mysql_free_result($query);
  220. }
  221. function fetch_fields($query) {
  222. return mysql_fetch_field($query);
  223. }
  224. function version($link) {
  225. return mysql_get_server_info($link);
  226. }
  227. function close($link) {
  228. return mysql_close($link);
  229. }
  230. }
  231. /* 测试例子 //
  232. * $dbarray[] = array('localhost','bevin','password','test');
  233. * $dbarray[] = array('ilaopo.net','root','password','test');
  234. * $dbarray[] = array('192.168.1.77','cxh','password','test');
  235. * $newdb = new mysqldb($dbarray,'utf8');
  236. * $array = array('name' => date("Y-m-d H:i:s"));
  237. * $id = $newdb->insert("test",$array);
  238. * $result = $newdb->fetch_page("select * from test order by id desc",'10');
  239. * print_r($result);
  240. * $id = $newdb->insert("test",$array);
  241. * echo $id;
  242. */

PS:为便于读者阅读源码,上述代码使用在线工具http://tools.jb51.net/code/jb51_php_format进行了格式化处理。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总》

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

人气教程排行