MySQL implements master-slave insert and query separate operations

Source: Internet
Author: User

Driver support for <?php/** * gmysql mysql database  */class gMysql {   /**     *  database link handle     */   public  $conn;//  The connection currently in use    /**    *  execution of SQL statement records     */    public  $ARRSQL;   //  Current connection id   public  $m _link_id = null;  //  Main Library Connections    public  $s _link_id = null; //  from library connections             //  whether multi-Library    public  $multi _server  = false;           //  database connection parameter configuration     public  $dbConfig  = array  ();    public  $dbCurrent  = array  ();    /**    *  Constructors      *    *  @param  dbConfig   Database Configuration     */   public function  __construct ($dbConfig)    {              $this->dbconfig =  $dbConfig;              $this->multi_server = empty  (  $this->dbconfig[' slave '] )  ? false : true;   }   /**    *  Connect database methods     *  @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 );                }      /**     *  Initializing database connections     *  @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;            }       }    }    /**    *  Get record results by SQL statement, return array     *    *  @param  sql   Execute SQL statement     */   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);   &Nbsp;   array_pop ($rows);      return  $rows;    }   /**    *  returns the primary key for the currently inserted record id    */    public function newinsertid ()    {      return  mysql_insert_id ($this->conn);   }   /**    *  Format the SQL statement with limit     */   public function setlimit ($sql,  $ Limit)    {      return  $sql .  " limit {$limit} ";   }   /**    *  executes an SQL statement     *     *  @param  sql  SQL statements to execute     */    Public function exec ($sql)    {       $this->arrsql[]  =  $sql;          $this->initconnect  ( true );             return mysql_query ($sql,  $this->conn);    }    /**     *  executes an SQL statement that is used primarily for querying       *  @param  type  $sql      *  @param  type  $master  default:false  true: Force read the main library, false: read from the library if there is a preference from the library, otherwise reading the main library      */     public function query  ($sql,  $master  = false)  {          $this->arrsql[] =  $sql;          $this->initconnect  (  $master  );         return mysql_query ($sql,  $this->conn);    }   /**     *  returns the number of affected rows &NBSp;   */   public function affected_rows ()    {       return mysql_affected_rows ($this->conn);   }    /**    *  Get Data sheet structure     *    *  @param  tbl_name   Table name     */   public function gettable ($ Tbl_name)    {      return  $this->getarray ("describe  {$tbl _name} ");    }       //prevent mysql gone away     public function ping ($master)  {       if ([ Email protected]_ping ($this->conn)) {          @mysql_close ($this->conn);  //Note: Be sure to perform a database shutdown first, which is the key          if ($master  | |  ! $thIs->multi_server)  {            unset ($ THIS-&GT;M_LINK_ID);         } else {             unset ($this->s_link_id);          }          $this->initconnect ($ Master);      }    }        /**    *  filtering of special characters     *    * @ param value   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);    }      /**    *   destructor     */   public function __destruct ()     {        if ( true != @ $this->dbcurrent[' persistent ')  ) @mysql_close ($this->conn);    }}

Focus on the Initconnect ($master) method, which determines whether the configuration file to be loaded is connected to the main library or from the library

MySQL implements master-slave insert and query separate operations

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.