PHP-enabled MySQL class for read and write separation

Source: Internet
Author: User
  1. /**

  2. * MySQL read/write separation class
  3. * $db _config = Array (
  4. * ' master ' + = Array (' host ' = ' localhost:3306 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
  5. * ' slave ' = = Array (
  6. * Array (' host ' = ' localhost:3307 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
  7. * Array (' host ' = ' localhost:3308 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat ')
  8. * )
  9. * );
  10. *
  11. * Note: If slave has multiple randomly connected one of the
  12. * Last edit: bbs.it-home.org
  13. */
  14. /*
  15. $db _config = Array (
  16. ' Master ' = = Array (' host ' = ' localhost:3306 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
  17. ' Slave ' = Array (
  18. Array (' host ' = ' localhost:3307 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
  19. Array (' host ' = ' localhost:3308 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' 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;//Primary Database Connection instance
  34. private static $_slave = null;//Database Connection instance
  35. Public $_config = Array ();//Database connection configuration information
  36. Public $_res = null;//Query Instance handle
  37. Public $_flag = ";//identifies whether the current statement is executed on a primary or a heavy 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. * Get 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 Specify the database name of the connection, by default the library that connects the configuration file
  66. * @param $mode RW means connecting to the main library, r-w for read-write separation
  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 primary 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 from 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. * Perform database queries
  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. * Get single-line records
  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. * Get multiple lines 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. * Gets 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. }

Copy Code
  • 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.