Using PHP to achieve MySQL read and write separation

Source: Internet
Author: User
Tags character set connect mysql query pconnect php code split mysql database

This code is modified from Uchome code because it is handled in order to solve the efficiency of uchome. This thinking is actually a long time, but has not been to do, I believe that some people have the same idea, if there is a similar, that really want to make relevant suggestions.

The encapsulation is simpler, increases the interface extension of the read-only database connection, does not use a read-only database, and does not affect the original code. To continue to improve the future.
For convenience, try setting up a Google project:
http://code.google.com/p/mysql-rw-php/
Hope to bring help to a friend in need.

PHP implementation of MySQL read and write separation
Main Features:
1. Simple Read and write separation
2. A primary database, you can add more read-only databases
3. Read and write separation but don't worry about some features not supported
4. Disadvantage: Connect two databases at the same time
English sucks, 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 in the same time
PHP Code:
mysql_rw_php.class.php

Copy Code code as follows:




<?php


/****************************************


* * * 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 {


//Query number


var $querynum = 0;


//Current operation of the 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
//uninitialized, the first connection is used as the 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= $this->charset, character_set_results= $this->charset, Character_set_client=binary ", $this->link);


}


if ($this->version () > ' 5.0.1 ') {


@mysql_query ("SET sql_mode=" ", $this->link);


}


}


if ($dbname) {


$this->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;


//does not produce halt error


$this->connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect, false);


if ($this->link) {


//Connection Successful


//echo "link ro sussess!<br>";


$this->ro_exist = true;


$this->link_ro = $this->link;


if ($this->cur_db) {


///If you have selected a database you will need to operate once


@mysql_select_db ($this->cur_db, $this->link_ro);


}


}else{


//Connection failure


//echo "link ro failed!<br>";


$this->link = & $this->link_rw;


}


}


Set up a series of read-only databases and connect one of the


function set_ro_list ($ro _list) {


if (Is_array ($ro _list)) {


//Randomly select one of the


$link _ro = $ro _list[array_rand ($ro _list)];


$this->connect_ro ($link _ro[' dbhost '), $link _ro[' Dbuser '], $link _ro[' DBPW ']);


}


}


function select_db ($dbname) {


//Simultaneous operation of two database connections


$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;


//Judge whether SELECT statement


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 "<div style=" Position:absolute;font-size:11px;font-family:verdana,arial;background: #EBEBEB;p adding:0 .5em; " >


<b>mysql error</b><br>


<b>message</b&gt: $message <br>


<b>sql</b&gt: $sql <br>


<b>error</b&gt: $dberror <br>


<b>errno.</b&gt: $dberrno <br>


</div> ";


exit ();


}


}


?>

Related Article

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.