溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MYSQL實現(xiàn)主從insert和query分開操作

發(fā)布時間:2020-06-28 21:51:04 來源:網(wǎng)絡 閱讀:393 作者:雷頓學院 欄目:數(shù)據(jù)庫
<?php
/**
 * gMysql MySQL數(shù)據(jù)庫的驅(qū)動支持
 */
class gMysql {
   /**
    * 數(shù)據(jù)庫鏈接句柄
    */
   public $conn;// 當前使用的連接
   /**
    * 執(zhí)行的SQL語句記錄
    */
   public $arrSql;

   // 當前連接ID
   public $m_link_id = null; // 主庫連接
   public $s_link_id = null; // 從庫連接
        
   // 是否多庫
   public $multi_server = false;
        
   // 數(shù)據(jù)庫連接參數(shù)配置
   public $dbConfig = array ();
    public $dbCurrent = array ();

    /**
    * 構(gòu)造函數(shù)
    *
    * @param dbConfig  數(shù)據(jù)庫配置
    */
   public function __construct($dbConfig)
   {
            $this->dbConfig = $dbConfig;
            $this->multi_server = empty ( $this->dbConfig['slave'] ) ? false : true;
   }
   /**
    * 連接數(shù)據(jù)庫方法
    * @param type $dbConfig 
    */
   public function connect ($db) {
      $this->dbCurrent = $db;
      $linkfunction = ( TRUE == $db['persistent'] ) ? 'mysql_pconnect' : 'mysql_connect';
      $this->conn = $linkfunction ( $db ['host'], $db ['username'], $db ['password']);

      if (! $this->conn) {
            return false;
      }
      $re = mysql_select_db($db['dbname'], $this->conn);
      if(!$re) {
         return false;
      }
      mysql_query ( "SET NAMES '" . $db ['charset'] . "'", $this->conn );            
   }
   
   /**
    * 初始化數(shù)據(jù)庫連接
    * @param type $master 
    */
   public function initConnect ($master = true) {
      if ($master || !$this->multi_server) {
            if($this->m_link_id){
                  $this->conn = $this->m_link_id;
                  $this->ping($master);
            } else {
                  $this->connect ( $this->dbConfig ['master'] );
                  $this->m_link_id = $this->conn;
            }
      } else {
            if($this->s_link_id){
                  $this->conn = $this->s_link_id;
                  $this->ping($master);
            } else {
                  $rand = rand(0, count($this->dbConfig ['slave']) - 1);
                  $this->connect ( $this->dbConfig ['slave'][$rand] );
                  $this->s_link_id = $this->conn;
            }
      }
    }

    /**
    * 按SQL語句獲取記錄結(jié)果,返回數(shù)組
    *
    * @param sql  執(zhí)行的SQL語句
    */
   public function getArray($sql)
   {
      if( ! $result = $this->query($sql) )return FALSE;
      if( ! mysql_num_rows($result) ) return FALSE;
      $rows = array();
      while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
      mysql_free_result($result);
      array_pop($rows);
      return $rows;
   }

   /**
    * 返回當前插入記錄的主鍵ID
    */
   public function newinsertid()
   {
      return mysql_insert_id($this->conn);
   }

   /**
    * 格式化帶limit的SQL語句
    */
   public function setlimit($sql, $limit)
   {
      return $sql. " LIMIT {$limit}";
   }

   /**
    * 執(zhí)行一個SQL語句
    *
    * @param sql 需要執(zhí)行的SQL語句
    */
   public function exec($sql)
   {
      $this->arrSql[] = $sql;
        $this->initConnect ( true );
      
      return mysql_query($sql, $this->conn);
   }
    /**
     * 執(zhí)行一個SQL語句,主要用于查詢
     * @param type $sql
     * @param type $master default:false 為true:強制讀主庫;為false:在有從庫的的情況下優(yōu)先讀從庫,否則讀主庫
     */
    public function query ($sql, $master = false) {
        $this->arrSql[] = $sql;
        $this->initConnect ( $master );
        return mysql_query($sql, $this->conn);
    }

   /**
    * 返回影響行數(shù)
    */
   public function affected_rows()
   {
      return mysql_affected_rows($this->conn);
   }

   /**
    * 獲取數(shù)據(jù)表結(jié)構(gòu)
    *
    * @param tbl_name  表名稱
    */
   public function getTable($tbl_name)
   {
      return $this->getArray("DESCRIBE {$tbl_name}");
   }
    
   //防止mysql gone away
    public function ping($master) {
       if(!@mysql_ping($this->conn)){
         @mysql_close($this->conn); //注意:一定要先執(zhí)行數(shù)據(jù)庫關(guān)閉,這是關(guān)鍵
         if($master || !$this->multi_server) {
            unset($this->m_link_id);
         } else {
            unset($this->s_link_id);
         }
         $this->initConnect($master);
      }
    }
    
   /**
    * 對特殊字符進行過濾
    *
    * @param value  值
    */
   public function __val_escape($value) {
      if(is_null($value))return 'NULL';
      if(is_bool($value))return $value ? 1 : 0;
      if(is_int($value))return (int)$value;
      if(is_float($value))return (float)$value;
      if(@get_magic_quotes_gpc())$value = stripslashes($value);
        $this->conn || $this->initConnect();
      return '\''.mysql_real_escape_string($value, $this->conn).'\'';
   }

   public function escape($value) {
      if(is_null($value))return 'NULL';
      if(is_bool($value))return $value ? 1 : 0;
      if(is_int($value))return (int)$value;
      if(is_float($value))return (float)$value;
      if(@get_magic_quotes_gpc())$value = stripslashes($value);
        $this->conn || $this->initConnect();
      return mysql_real_escape_string($value, $this->conn);
   }
   
   /**
    * 析構(gòu)函數(shù)
    */
   public function __destruct()
   {
        if( TRUE != @$this->dbCurrent['persistent'] )@mysql_close($this->conn);
   }
}

重點在 initConnect($master)方法里,這里決定加載的配置文件中是連接到主庫還是從庫

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI