ホームページ  >  記事  >  バックエンド開発  >  読み取りと書き込みの分離をサポートする PHP によって実装された MySQL クラス

読み取りと書き込みの分離をサポートする PHP によって実装された MySQL クラス

WBOY
WBOYオリジナル
2016-07-25 08:56:07856ブラウズ
  1. /**

  2. * MySQL 読み書き分離クラス
  3. * $db_config = array(
  4. * 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd' = >'123456','db'=>'stat'),
  5. * 'スレーブ' => array('host'=>'localhost:3307','user'=> ' admin','passwd'=>'123456','db'=>'stat'),
  6. * array('host'=>'localhost:3308','user'=>'admin' , 'passwd'=>'123456','db'=>'stat')
  7. * )
  8. * );
  9. *
  10. * 注: 複数のスレーブがある場合は、そのうちの 1 つをランダムに接続します
  11. * 最終編集: bbs .it-home.org
  12. */
  13. /*
  14. $db_config = array(
  15. 'master' => array('host'=>'localhost: 3306','user'=>'admin','passwd'=>'123456','db'=>'stat'),
  16. 'slave' => array(
  17. array('host'= >'localhost:3307','user'=>'admin','passwd'=>'123456','db'=>'stat'),
  18. array('host'=>'localhost】 :3308','user'=>'admin','passwd'=>'123456','db'=>'stat')
  19. )
  20. );

  21. $ db = MySQL::getInstance('','r-w');

  22. $sql = "select * from admin";

  23. $rs = $db->query($sql);

  24. while ($row = $db->fetch($rs)){
  25. echo "uid:".$row['uid']." ".$row[ 'userName']."
    ";
  26. }

  27. echo "


    ";
  28. */

  29. class MySQL

  30. {
  31. private static $_instance = null;//データベース库接続例
  32. private static $_master = null;// 主データベース库接続例
  33. private static $_slave = null;// 重データベース库接続例
  34. public $_config = array();//データ库接続構成情報
  35. public $_res = null;//查询实例句
  36. public $_flag = '';//标识当前语句是主还是重データ台库上行
  37. public $_link = null;
  38. /**
  39. * 単一インスタンス
  40. * ここに説明を入力します ...
  41. * @paramknown_type $dbname
  42. * @paramknown_type $mode
  43. */
  44. public static function & getInstance($dbname='',$mode='rw'){
  45. if (is_null(self::$_instance)){
  46. self::$_instance = 新しい self();
  47. self::$_instance->__getConf();
  48. self::$_instance->connect($dbname,$mode);
  49. }
  50. return self::$_instance;
  51. }

  52. /**

  53. * データベース構成情報を取得します
  54. * ここに説明を入力します...
  55. */
  56. public function __getConf(){
  57. global $db_config;
  58. $this->_config['master'] = $db_config['master'];
  59. $this->_config['スレーブ'] = $db_config['slave'];
  60. }
  61. /**
  62. * データベース接続
  63. * ここに説明を入力します...
  64. * @param $dbname は、デフォルトでは、設定ファイルのライブラリが接続されます
  65. * @param $mode rw は、メイン ライブラリに接続することを意味します。 、r-w は読み取りと書き込みの分離を意味します
  66. */
  67. public function connect($dbname='',$mode = 'rw'){
  68. if($mode == 'rw'){
  69. if(is_null(self::$_master)){
  70. $this->_master = $this->_slave = $this->conn_master($dbname);
  71. }
  72. }else{
  73. if(is_null(self::$_master)){
  74. $this->_master = $this->conn_master($dbname);
  75. }
  76. if(is_null(self::$_slave)){
  77. $this ->_slave = $this->conn_slave($dbname);
  78. }
  79. }
  80. }
  81. /**
  82. * メインデータベースサーバーに接続します
  83. * ここに説明を入力してください...
  84. */
  85. public function conn_master($dbname=''){
  86. $_link = mysql_connect( $this->_config['master']['host'],$this->_config['master']['user'],$this->_config['master']['passwd'] ,true) または die ("Connect ".$this->_config['master']['host']." 失敗します。");
  87. mysql_select_db(empty($dbname)?$this->_config[' master']['db']:$dbname,$_link) または die(" DB 名 ".$this->_config['master']['db']." は存在しません。");
  88. mysql_query("set names utf8",$_link);
  89. return $_link;
  90. }
  91. /**
  92. * スレーブデータベースサーバーに接続します
  93. * ここに説明を入力してください...
  94. */
  95. public function conn_slave($dbname=''){
  96. $offset = rand(0,count($this->_config['slave'])-1);
  97. $ _link = @mysql_connect($this->_config['slave'][$offset]['host'],$this->_config['slave'][$offset]['user'],$this- >_config['slave'][$offset]['passwd'],true) または die(" Connect ".$this->_config['slave'][$offset]['host']." 失敗します.");
  98. mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$_link) または die(" DB 名 ".$ this->_config['slave'][$offset]['db']." は存在しません。");
  99. mysql_query("set names utf8",$_link);
  100. return $_link;
  101. }< /p>
  102. /**

  103. * データベースクエリを実行します
  104. * ここに説明を入力します...
  105. * @param string $sql
  106. */
  107. パブリック関数クエリ($sql,$master=true){

  108. if($master == true || (substr(strto lower($sql),0,6) != 'select') && $master == false){

  109. $this->_res = mysql_query($sql,$ this->_master);
  110. if(!$this->_res){
  111. $this->_error[] = mysql_error($this->_master);
  112. }
  113. $this->_flag = ' master';
  114. $this->gt;_link = $this->gt;_master;
  115. } else {
  116. $this->gt;_res = mysql_query($sql,$this->_slave);
  117. if(!$this- >_res){
  118. $this->_error[] = mysql_error($this->_slave);
  119. }
  120. $this->_flag = 'スレーブ';
  121. $this->_link = $this- >_slave;
  122. }

  123. return $this->_res;

  124. }
  125. /**
  126. * 単一行のレコードを取得します
  127. * ここに説明を入力します ...
  128. * @parammixed $rs
  129. */
  130. public function get($rs=''){
  131. if(empty($rs)){
  132. $rs = $this-> ;_res;
  133. }
  134. return mysql_fetch_row($rs);
  135. }
  136. /**
  137. * 複数行のレコードを取得します
  138. * ここに説明を入力します...
  139. * @parammixed $rs
  140. * @param $result_type
  141. */
  142. public function fetch($rs = ''){
  143. if(empty($rs)){
  144. $rs = $this->_res;
  145. }
  146. return mysql_fetch_array($rs,MYSQL_ASSOC);
  147. }
  148. /**
  149. * データを挿入します
  150. * ここに説明を入力します ...
  151. * @paramknown_type $sql
  152. */
  153. public function add($sql){
  154. $rs = $this-> query($sql);
  155. if($rs)
  156. return mysql_insert_id($this->_link);
  157. return false;
  158. }
  159. /**
  160. * データを更新します
  161. * ここに説明を入力します...
  162. * @paramknown_type $sql
  163. */
  164. public function update($sql){
  165. if(empty($sql)) return false;
  166. $rs = $this->query($sql);
  167. if($rs)
  168. return $this->fetchNum();
  169. return false;
  170. }< ;/p>
  171. /**

  172. * 前のステートメントの影響を受ける行数を取得します
  173. * ここに説明を入力します...
  174. */
  175. public function fetchNum(){
  176. return mysql_affected_rows($this->_link);
  177. }
  178. /**
  179. * デストラクター、データベース接続リソースを解放します
  180. * ここに説明を入力します...
  181. */
  182. public function __destruct(){
  183. mysql_close($ this->_link);
  184. }
  185. }

复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。