本文实例讲述了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['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') "/&","",$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') " `$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 "SELECT last_insert_id()"), 0);
  }
  // db other
  function affected_rows($link) {
    return mysql_affected_rows($link);
  }
  function error($link) {
    return (($link) "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.jb51.net/code/jb51_php_format进行了格式化处理。

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

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

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。