Mysql read/write splitting implemented by PHP

Source: Internet
Author: User

This code is modified from the uchome code because it needs to solve the uchome efficiency. This thinking has actually existed for a long time, but it has never been done. I believe some people have the same idea. If there is a similar idea, I really hope to put forward relevant suggestions.

The encapsulation method is simple, and the interface extension of the read-only database connection is added. The use of the original code is not affected if the read-only database is not used. It remains to be improved in the future ..

For convenience, I tried to build a google Project.:
Http://code.google.com/p/mysql-rw-php/

Hope to help friends in need.

Mysql read/write splitting implemented by PHP

Main features:

  1. Simple read/write splitting
  2. You can add more read-only databases to a primary database.
  3. Read/write splitting, but do not worry about unsupported features
  4. Disadvantage: connecting two databases at the same time

Poor English. Just 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

<? Php
/*************************************** *
* ** Mysql-rw-php version 0.1 @ 2009-4-16
* ** 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;
// The current database connection
Var $ link = null;
// Character Set
Var $ charset;
// Current Database
Var $ cur_db = '';
// Check whether a valid read-only database connection exists
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;

// If rw is not initialized, the first connection acts as 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 to 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
$ 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 already selected a database, you need to perform the operation once.
@ Mysql_select_db ($ this-> cur_db, $ this-> link_ro );
}
} Else {
// Connection failed
// Echo "link ro failed! <Br> ";
$ This-> link = & $ this-> link_rw;
}
}
// Set a series of read-only databases and connect to one of them
Function set_ro_list ($ ro_list ){
If (is_array ($ ro_list )){
// Select one of them randomly
$ 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 the connection between the two databases 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;
// Determine whether a select statement is used
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; padding: 0.5em; \ ">
<B> MySQL Error </B> <br>
<B> Message </B>: $ message <br>
<B> SQL </B>: $ SQL <br>
<B> Error </B>: $ dberror <br>
<B> Errno. </B>: $ dberrno <br>
</Div> ";
Exit ();
}
}
?>

Example. php

<? Php
/*************************************** *
* ** Mysql-rw-php version 0.1 @ 2009-4-16
* ** Code by hqlulu # gmail.com
* ** Http://www.aslibra.com
* ** Http://code.google.com/p/mysql-rw-php/
* ** Code modify from class_mysql.php (uchome)
****************************************/
Require_once ('mysql _ rw_php.class.php ');
// Rw info
$ Db_rw = array (
'Dbhost' => 'www .aslibra.com ',
'Dbuser' => 'aslibra ',
'Dbpw '=> 'www .aslibra.com ',
'Dbname' => 'test'
);
$ Db_ro = array (
Array (
'Dbhost' => 'www .aslibra.com: 8080 ',
'Dbuser' => 'aslibra ',
'Dbpw '=> 'www .aslibra.com'
)
);
$ DB = new mysql_rw_php;
// Connect Master
$ DB-> connect ($ db_rw [dbhost], $ db_rw [dbuser], $ db_rw [dbpw], $ db_rw [dbname]);
// Method 1: connect one server
$ DB-> connect_ro ($ db_ro [0] [dbhost], $ db_ro [0] [dbuser], $ db_ro [0] [dbpw]);
// Method 2: connect one server from a list by rand
$ DB-> set_ro_list ($ db_ro );
// Send to rw
$ SQL = "insert into a set a = 'test '";
$ DB-> query ($ SQL );
// Send to ro
$ SQL = "select * from ";
$ Qr = $ DB-> query ($ SQL );
While ($ row = $ DB-> fetch_array ($ qr )){
Echo $ row [a];
}
?>

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.