MySQL class that supports read/write splitting implemented by php

Source: Internet
Author: User
MySQL class that supports read/write splitting implemented by php

  1. /**

  2. * MySQL read/write splitting
  3. * $ Db_config = array (
  4. * 'Master' => array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
  5. * 'Slave '=> array (
  6. * Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
  7. * Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat ')
  8. *)
  9. *);
  10. *
  11. * Note: If there are multiple slave instances, one of them is randomly connected.
  12. * Last edit: bbs.it-home.org
  13. */
  14. /*
  15. $ Db_config = array (
  16. 'Master' => array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
  17. 'Slave '=> array (
  18. Array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '2016 ', 'DB' => 'Stat '),
  19. Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123', 'DB' => 'Stat ')
  20. )
  21. );

  22. $ Db = MySQL: getInstance ('', 'R-W ');

  23. $ SQL = "select * from admin ";

  24. $ Rs = $ db-> query ($ SQL );

  25. While ($ row = $ db-> fetch ($ rs )){
  26. Echo "uid:". $ row ['uid']. "". $ row ['username']."
    ";
  27. }

  28. Echo"

    ";
  29. */

  30. Class MySQL

  31. {
  32. Private static $ _ instance = null; // database connection instance
  33. Private static $ _ master = null; // the master database connects to the instance.
  34. Private static $ _ slave = null; // reconnect to the instance
  35. Public $ _ config = array (); // database connection configuration information
  36. Public $ _ res = null; // query the instance handle
  37. Public $ _ flag = ''; // identifies whether the current statement is executed on the master or database.
  38. Public $ _ link = null;
  39. /**
  40. * Single instance
  41. * Enter description here...
  42. * @ Param unknown_type $ dbname
  43. * @ Param unknown_type $ mode
  44. */
  45. Public static function & getInstance ($ dbname = '', $ mode = 'rw '){
  46. If (is_null (self ::$ _ instance )){
  47. Self: $ _ instance = new self ();
  48. Self: $ _ instance->__ getConf ();
  49. Self: $ _ instance-> connect ($ dbname, $ mode );
  50. }
  51. Return self: $ _ instance;
  52. }

  53. /**

  54. * Obtain database configuration information
  55. * Enter description here...
  56. */
  57. Public function _ getConf (){
  58. Global $ db_config;
  59. $ This-> _ config ['master'] = $ db_config ['master'];
  60. $ This-> _ config ['Slave '] = $ db_config ['Slave'];
  61. }
  62. /**
  63. * Database connection
  64. * Enter description here...
  65. * @ Param $ dbname: specifies the name of the connected Database. by default, the database of the configuration file is connected.
  66. * @ Param $ mode rw indicates connection to the master database, and r-w indicates read/write splitting.
  67. */
  68. Public function connect ($ dbname = '', $ mode = 'rw '){
  69. If ($ mode = 'rw '){
  70. If (is_null (self ::$ _ master )){
  71. $ This-> _ master = $ this-> _ slave = $ this-> conn_master ($ dbname );
  72. }
  73. } Else {
  74. If (is_null (self ::$ _ master )){
  75. $ This-> _ master = $ this-> conn_master ($ dbname );
  76. }
  77. If (is_null (self ::$ _ slave )){
  78. $ This-> _ slave = $ this-> conn_slave ($ dbname );
  79. }
  80. }
  81. }
  82. /**
  83. * Connect to the master database server
  84. * Enter description here...
  85. */
  86. Public function conn_master ($ dbname = ''){
  87. $ _ Link = mysql_connect ($ this-> _ config ['master'] ['host'], $ this-> _ config ['master'] ['user'], $ this-> _ config ['master'] ['passwd'], true) or die ("Connect ". $ this-> _ config ['master'] ['host']. "fail. ");
  88. Mysql_select_db (empty ($ dbname )? $ This-> _ config ['master'] ['DB']: $ dbname, $ _ link) or die ("The db name ". $ this-> _ config ['master'] ['DB']. "is not exists. ");
  89. Mysql_query ("set names utf8", $ _ link );
  90. Return $ _ link;
  91. }
  92. /**
  93. * Connect to the slave database server
  94. * Enter description here...
  95. */
  96. Public function conn_slave ($ dbname = ''){
  97. $ Offset = rand (0, count ($ this-> _ config ['Slave '])-1 );
  98. $ _ Link = @ mysql_connect ($ this-> _ config ['Slave '] [$ offset] ['host'], $ this-> _ config ['Slave '] [$ offset] ['user'], $ this-> _ config ['Slave '] [$ offset] ['passwd'], true) or die ("Connect ". $ this-> _ config ['Slave '] [$ offset] ['host']. "fail. ");
  99. Mysql_select_db (empty ($ dbname )? $ This-> _ config ['Slave '] [$ offset] ['DB']: $ dbname, $ _ link) or die ("The db name ". $ this-> _ config ['Slave '] [$ offset] ['DB']. "is not exists. ");
  100. Mysql_query ("set names utf8", $ _ link );
  101. Return $ _ link;
  102. }

  103. /**

  104. * Execute database query
  105. * Enter description here...
  106. * @ Param string $ SQL
  107. */
  108. Public function query ($ SQL, $ master = true ){

  109. If ($ master = true | (substr (strtolower ($ SQL), 0, 6 )! = 'Select') & $ master = false ){

  110. $ This-> _ res = mysql_query ($ SQL, $ this-> _ master );
  111. If (! $ This-> _ res ){
  112. $ This-> _ error [] = mysql_error ($ this-> _ master );
  113. }
  114. $ This-> _ flag = 'master ';
  115. $ This-> _ link = $ this-> _ master;
  116. } Else {
  117. $ This-> _ res = mysql_query ($ SQL, $ this-> _ slave );
  118. If (! $ This-> _ res ){
  119. $ This-> _ error [] = mysql_error ($ this-> _ slave );
  120. }
  121. $ This-> _ flag = 'Slave ';
  122. $ This-> _ link = $ this-> _ slave;
  123. }

  124. Return $ this-> _ res;

  125. }
  126. /**
  127. * Obtain a single row record
  128. * Enter description here...
  129. * @ Param mixed $ rs
  130. */
  131. Public function get ($ rs = ''){
  132. If (empty ($ rs )){
  133. $ Rs = $ this-> _ res;
  134. }
  135. Return mysql_fetch_row ($ rs );
  136. }
  137. /**
  138. * Obtain multiple rows of records
  139. * Enter description here...
  140. * @ Param mixed $ rs
  141. * @ Param $ result_type
  142. */
  143. Public function fetch ($ rs = ''){
  144. If (empty ($ rs )){
  145. $ Rs = $ this-> _ res;
  146. }
  147. Return mysql_fetch_array ($ rs, MYSQL_ASSOC );
  148. }
  149. /**
  150. * Insert data
  151. * Enter description here...
  152. * @ Param unknown_type $ SQL
  153. */
  154. Public function add ($ SQL ){
  155. $ Rs = $ this-> query ($ SQL );
  156. If ($ rs)
  157. Return mysql_insert_id ($ this-> _ link );
  158. Return false;
  159. }
  160. /**
  161. * Update data
  162. * Enter description here...
  163. * @ Param unknown_type $ SQL
  164. */
  165. Public function update ($ SQL ){
  166. If (empty ($ SQL) return false;
  167. $ Rs = $ this-> query ($ SQL );
  168. If ($ rs)
  169. Return $ this-> fetchNum ();
  170. Return false;
  171. }

  172. /**

  173. * Obtain the number of rows affected by the previous statement.
  174. * Enter description here...
  175. */
  176. Public function fetchNum (){
  177. Return mysql_affected_rows ($ this-> _ link );
  178. }
  179. /**
  180. * Destructor to release database connection resources
  181. * Enter description here...
  182. */
  183. Public function _ destruct (){
  184. Mysql_close ($ this-> _ link );
  185. }
  186. }

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.