PHP implementation of MySQLdb read-write separation Operation class

Source: Internet
Author: User
Tags pconnect
This article mainly introduces the PHP implementation of the MYSQLDB read and write separation operation class, combined with the instance form of PHP read and write to the database of the implementation skills, and give the package of the specific use of the class, the need for friends can refer to the next

This paper describes the PHP implementation of MYSQLDB read-write separation Operation class. Share to everyone for your reference, as follows:

/*** php mysqldb Read/write separation class *-----------------------------------------------------* $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 "Database parameter Error";    return false;    } $this->charset = $dbcharset;    $this->pconnect = $pconnect;    $this->dbconfig[' master ' = $dbarray [0];    $dbServerNum = count ($dbarray);      if ($dbServerNum > 1) {/* * when x=1, $dbarray [0] does not slave * when x=0, $dbarray [0] as 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 ("Primary database connection failed!    "); }//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-&GT;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-&GT;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);    }/* Gets the paging method */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;      /* Get Data result set */for ($i =0; $i < ($limitstart + $pagenum), $i + +) {if ($i >= $countnum) {break;      } if ($i >= $limitstart) {$result [] = $this->fetch_array ($query);      } else {$this->fetch_array ($query);    }//* Generate 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-&GT;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-&gt  ; 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); }}/* Test Example//* $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 ","); * Print_r ($result); * $id = $newdb->insert ("test", $array); * echo $id; 

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.