A file is displayed on the Internet, which means PHP implements separate connection between database update and database query.
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. Disadvantages: connecting two databases at the same time
Mysql_rw_php.class.php:
<? PHP /************************************** * *** mysql-RW-PHP version 0.1 @ 2009-4-16 *** code by hqlulu # gmail.com *** http://www.aslibra.com *** modify 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; // The current database var $ cur_db = ''; // whether a valid read-only database exists 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') ;}// if (! $ This-> link &&! $ Halt) return false; // when 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-> Sele Ct_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 occurs $ 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; }}// sets a series of read-only databases and connects to one of the functions 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 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 @ mysq Rochelle 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 to select the 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: # ebeb; 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 *** modify 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: 100 ', '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 a"; $ QR = $ db-> query ($ SQL ); while ($ ROW = $ db-> fetch_array ($ QR) {echo $ row [a] ;}?>