Home  >  Article  >  Backend Development  >  Using PHP to implement Mysql read and write separation_PHP tutorial

Using PHP to implement Mysql read and write separation_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:02:12904browse

This code is modified from uchome’s code and is processed to improve the efficiency of uchome. I have actually had this idea for a long time, but I have never done it. I believe there are people who have the same idea. If there are similar ideas, I really hope to provide relevant suggestions.

The encapsulation method is relatively simple, adding an interface extension for read-only database connection. Not using a read-only database does not affect the use of the original code. It remains to be continuously improved in the future. .
For convenience, try setting up a Google project:
http://code.google.com/p/mysql-rw-php/
I hope it will help friends in need.

Mysql read-write separation implemented by PHP
Main features:
1. Simple read-write separation
2. One main database, you can add more Read the database
3. Separate reading and writing but don’t worry that some features are not supported
4. Disadvantage: Connect two databases at the same time
English is poor, please write a few words
php code for mysql read/write split
feature:
simply rw split
one master,can add more slaves
support all mysql feature
link to the master and slave at the same time
PHP code:
mysql_rw_php.class.php

Copy code The code is as follows:

/****************************************
*** mysql-rw-php version 0.1
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/mysql-rw-php/
*** code modify from class_mysql.php (uchome)
****************************************/
class mysql_rw_php {
//Number of queries
var $querynum = 0;
//Current Operational database connection
var $link = null;
//Character set
var $charset;
//Current database
var $cur_db = '';
//Whether There is a valid read-only database connection
var $ro_exist = false;
//Read-only database connection
var $link_ro = null;
//Read-write database connection
var $link_rw = null;
function mysql_rw_php(){
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
if ($pconnect) {
if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
$halt && $this->halt('Can not connect to MySQL server');
}
} else {
if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
$halt && $this ->halt('Can not connect to MySQL server');
}
}

//Read-only connection failed
if(!$this->link && !$ halt) return false;

//When rw is not initialized, the first connection is rw
if($this->link_rw == null)
$this->link_rw = $ this->link;
if($this->version() > '4.1') {
if($this->charset) {
@mysql_query("SET character_set_connection=$ '5.0. 1') {
         @mysql_query("SET sql_mode=''", $this->link);
                                                                                                                          ;select_db($dbname);
}
}
//Connect a read-only mysql database
function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){
if($this->link_rw == null)
$this->link_rw = $this->link;
$this->link = null;
//No halt error is generated
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, false);
if($this->link){
//Connection successful
//echo "link ro sussess!
";
;
if($this->cur_db){
//If you have already selected a database, you need to operate it once
@mysql_select_db($this->cur_db, $this->link_ro);
}
}else{
                                                                                                                                                                                                        ;
}
}
//Set up a series of read-only databases and connect to one of them
function set_ro_list($ro_list){
if(is_array($ro_list)){
/ /Randomly select one of them
$link_ro = $ro_list[array_rand($ro_list)];
$this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro[ 'dbpw']);
}
}
function select_db($dbname) {
//Operate two database connections at the same time
$this->cur_db = $dbname;
if($this->ro_exist){
@mysql_select_db($dbname, $this->link_ro);
}
return @mysql_select_db($dbname, $this->link_rw);
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
  function fetch_one_array($sql, $type = '') {
    $qr = $this->query($sql, $type);
    return $this->fetch_array($qr);
  }
  function query($sql, $type = '') {
    $this->link = &$this->link_rw;
    //判断是否select语句
    if($this->ro_exist && preg_match ("/^(s*)select/i", $sql)){
      $this->link = &$this->link_ro;
    }
    $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
      'mysql_unbuffered_query' : 'mysql_query';
    if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
      $this->halt('MySQL Query Error', $sql);
    }
    $this->querynum++;
    return $query;
  }
  function affected_rows() {
    return mysql_affected_rows($this->link);
  }
  function error() {
    return (($this->link) ? mysql_error($this->link) : mysql_error());
  }
  function errno() {
    return intval(($this->link) ? mysql_errno($this->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 insert_id() {
    return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  }
  function fetch_row($query) {
    $query = mysql_fetch_row($query);
    return $query;
  }
  function fetch_fields($query) {
    return mysql_fetch_field($query);
  }
  function version() {
    return mysql_get_server_info($this->link);
  }
  function close() {
    return mysql_close($this->link);
  }
  function halt($message = '', $sql = '') {
    $dberror = $this->error();
    $dberrno = $this->errno();
    echo "

        MySQL Error

        Message: $message

        SQL: $sql

        Error: $dberror

        Errno.: $dberrno

       
";
    exit();
  }
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327936.htmlTechArticle本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的。这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn