首页 > 后端开发 > php教程 > php实现的mysqldb读写分离操作类

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

墨辰丷
发布: 2023-03-28 07:36:02
原创
1768 人浏览过

这篇文章主要介绍了php实现的mysqldb读写分离操作类,结合实例形式分析了php针对数据库的读写分离操作实现技巧,并给出了该封装类的具体使用方法,需要的朋友可以参考下

本文实例讲述了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 "<br>##connectM!.<br>";
  }
  function connectS() {
    if($this->dbconfig[&#39;slave&#39;]) {
      if(!$this->linkr = $this->connect($this->dbconfig[&#39;slave&#39;][0],$this->dbconfig[&#39;slave&#39;][1],$this->dbconfig[&#39;slave&#39;][2],$this->dbconfig[&#39;slave&#39;][3])) {
        $this->dbconfig[&#39;slave&#39;] = false;
        $this->connectS();
      }
      //echo "<br>##connectS!.<br>";
    } else {
      if(!$this->linkw) {
        $this->connectM();
      }
      $this->linkr = $this->linkw;
    }
  }
  // db read
  function query($sql, $type = &#39;&#39;) {
    if(!$this->linkr) {
      $this->connectS();
    }
    $func = $type == &#39;UNBUFFERED&#39; && @function_exists(&#39;mysql_unbuffered_query&#39;) ?
      &#39;mysql_unbuffered_query&#39; : &#39;mysql_query&#39;;
    $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[&#39;page&#39;]);
    $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[&#39;QUERY_STRING&#39;];
    $url = preg_replace("/&?page=[0-9]+/i","",$url);
    $array[&#39;countnum&#39;] = $countnum;
    $array[&#39;countpage&#39;] = $countpage;
    $array[&#39;result&#39;] = $result;
    $array[&#39;page&#39;] = $page;
    if($page>1) {
      $array[&#39;preurl&#39;] = "?".$url."&page=".($page-1);
      $array[&#39;prepage&#39;] = $page-1;
    } else {
      $array[&#39;preurl&#39;] = "?".$url."&page=1";
      $array[&#39;prepage&#39;] = 1;
    }
    if($page<$countpage) {
      $array[&#39;nexturl&#39;] = "?".$url."&page=".($page+1);
      $array[&#39;nextpage&#39;] = $page+1;
    } else {
      $array[&#39;nexturl&#39;] = "?".$url."&page=".$countpage;
      $array[&#39;nextpage&#39;] = $countpage;
    }
    $array[&#39;firsturl&#39;] = "?".$url."&page=1";
    $array[&#39;firstpage&#39;] = 1;
    $array[&#39;lasturl&#39;] = "?".$url."&page=".$countpage;
    $array[&#39;lastpage&#39;] = $countpage;
    $array[&#39;nopage&#39;] = "?".$url."&page=";
    return $array;
  }
  // db write
  function queryw($sql, $type = &#39;&#39;) {
    if(!$this->linkw) {
      $this->connectM();
    }
    $func = $type == &#39;UNBUFFERED&#39; && @function_exists(&#39;mysql_unbuffered_query&#39;) ?
      &#39;mysql_unbuffered_query&#39; : &#39;mysql_query&#39;;
    $query = $func($sql, $this->linkw);
    $this->querynum++;
    return $query;
  }
  function update($table,$where,$array) {
    $whereset = $set = $dot = &#39;&#39;;
    foreach($array as $key=>$value) {
      $set .= $dot." `$key`=&#39;$value&#39;";
      $dot = &#39;,&#39;;
    }
    $whereset = $dot = &#39;&#39;;
    foreach($where as $k=>$v) {
      $whereset .= $dot." `$k`=&#39;$v&#39;";
      $dot = &#39; and &#39;;
    }
    $sql = "update $table set $set where $whereset";
    return $this->queryw($sql);
  }
  function insert($table,$array) {
    $col = $v = $dot = &#39;&#39;;
    foreach($array as $key=>$value) {
      $v .= $dot."&#39;$value&#39;";
      $col .= $dot."`$key`";
      $dot = &#39;,&#39;;
    }
    $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(&#39;localhost&#39;,&#39;bevin&#39;,&#39;password&#39;,&#39;test&#39;);
* $dbarray[] = array(&#39;ilaopo.net&#39;,&#39;root&#39;,&#39;password&#39;,&#39;test&#39;);
* $dbarray[] = array(&#39;192.168.1.77&#39;,&#39;cxh&#39;,&#39;password&#39;,&#39;test&#39;);
* $newdb = new mysqldb($dbarray,&#39;utf8&#39;);
* $array = array(&#39;name&#39; => date("Y-m-d H:i:s"));
* $id = $newdb->insert("test",$array);
* $result = $newdb->fetch_page("select * from test order by id desc",&#39;10&#39;);
* print_r($result);
* $id = $newdb->insert("test",$array);
* echo $id;
*/
登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

Python实现封装MySQLdb基本操作类

python之mysqldb模块在windows下安装方法

Python环境下安装MySQLdb模块

以上是php实现的mysqldb读写分离操作类的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板