侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   180

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

/*** 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 "
##connectM!.
"; } 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 "
##connectS!.
"; } 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;*/

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

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

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

您可能感兴趣的文章:

  • Thinkphp实现MySQL读写分离操作示例
  • 使用PHP实现Mysql读写分离
  • PHP+MYSQL实现读写分离简单实战
  • php实现带读写分离功能的MySQL类完整实例
  • thinkphp下MySQL数据库读写分离代码剖析
  • PHP+MYSQL实现用户的增删改查
  • PHP连接和操作MySQL数据库基础教程
  • php mysql数据库操作类
  • PHP实现的mysql读写分离操作示例


  • 上一条:
    php实现的读取CSV文件函数示例
    下一条:
    PHP验证终端类型是否为手机的简单实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客