Example of mysqldb read/write Splitting Operation implemented by php and mysqldb

Source: Internet
Author: User

Example of mysqldb read/write Splitting Operation implemented by php and mysqldb

This example describes the mysqldb read/write Splitting Operation class implemented by php. We will share this with you for your reference. The details are as follows:

/*** Php MysqlDB read/write splitting class ** --------------------------------------------------- * $ Source: http://code.ilaopo.net/php.class.mysqldb $ * $ Author: Bevin Chen $ * $ Email: bevin # lifa8.cn $ * $ Date: 2009-10-10 $ * callback */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] is not slave * when x = 0, $ dbarray [0] is 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 ("master 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-> 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-> 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);}/* Get 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) {$ resu Lt [] = $ 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-> 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 whe Re $ 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-> 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 ('2017. 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 ", '10'); * print_r ($ result); * $ id = $ newdb-> insert ("test", $ array); * echo $ id ;*/

PS:To facilitate readers to read the source code, the above Code uses the online tool http://tools.jb51.net/code/jb51_php_formatfor formatting.

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.