/**
- * MySQL read/write separation class
- * $db _config = Array (
- * ' master ' + = Array (' host ' = ' localhost:3306 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
- * ' slave ' = = Array (
- * Array (' host ' = ' localhost:3307 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
- * Array (' host ' = ' localhost:3308 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat ')
- * )
- * );
- *
- * Note: If slave has multiple randomly connected one of the
- * Last edit: bbs.it-home.org
- */
- /*
- $db _config = Array (
- ' Master ' = = Array (' host ' = ' localhost:3306 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
- ' Slave ' = Array (
- Array (' host ' = ' localhost:3307 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat '),
- Array (' host ' = ' localhost:3308 ', ' user ' = ' admin ', ' passwd ' = ' 123456 ', ' db ' = ' stat ')
- )
- );
$db = Mysql::getinstance (' ', ' r-w ');
$sql = "SELECT * from admin";
$rs = $db->query ($sql);
- while ($row = $db->fetch ($rs)) {
- echo "UID:". $row [' uid ']. " ". $row [' UserName ']."
";
- }
echo " ";
- */
Class MySQL
- {
- private static $_instance = null;//Database Connection instance
- private static $_master = null;//Primary Database Connection instance
- private static $_slave = null;//Database Connection instance
- Public $_config = Array ();//Database connection configuration information
- Public $_res = null;//Query Instance handle
- Public $_flag = ";//identifies whether the current statement is executed on a primary or a heavy database
- public $_link = null;
- /**
- * Single Instance
- * Enter description here ...
- * @param unknown_type $dbname
- * @param unknown_type $mode
- */
- public static function & getinstance ($dbname = ', $mode = ' rw ') {
- if (Is_null (self::$_instance)) {
- Self::$_instance = new self ();
- Self::$_instance->__getconf ();
- Self::$_instance->connect ($dbname, $mode);
- }
- return self::$_instance;
- }
/**
- * Get Database configuration information
- * Enter description here ...
- */
- Public Function __getconf () {
- Global $db _config;
- $this->_config[' master '] = $db _config[' master ');
- $this->_config[' slave ') = $db _config[' slave '];
- }
- /**
- * Database connection
- * Enter description here ...
- * @param $dbname Specify the database name of the connection, by default the library that connects the configuration file
- * @param $mode RW means connecting to the main library, r-w for read-write separation
- */
- Public Function Connect ($dbname = ', $mode = ' rw ') {
- if ($mode = = ' rw ') {
- if (Is_null (Self::$_master)) {
- $this->_master = $this->_slave = $this->conn_master ($dbname);
- }
- }else{
- if (Is_null (Self::$_master)) {
- $this->_master = $this->conn_master ($dbname);
- }
- if (Is_null (Self::$_slave)) {
- $this->_slave = $this->conn_slave ($dbname);
- }
- }
- }
- /**
- * Connect to the primary database server
- * Enter description here ...
- */
- Public Function conn_master ($dbname = ") {
- $_link = mysql_connect ($this->_config[' master ' [' Host '], $this->_config[' master ' [' User '], $this->_config [' Master '] [' passwd '],true) or Die ("Connect". $this->_config[' master '] [' host ']. "fail.");
- mysql_select_db (Empty ($dbname)? $this->_config[' master ' [' DB ']: $dbname, $_link) or Die ("the DB name". $this->_ config[' master ' [' DB ']. "is not exists.");
- mysql_query ("Set names UTF8", $_link);
- return $_link;
- }
- /**
- * Connect to from database server
- * Enter description here ...
- */
- Public Function conn_slave ($dbname = ") {
- $offset = rand (0,count ($this->_config[' slave '))-1);
- $_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.");
- 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.");
- mysql_query ("Set names UTF8", $_link);
- return $_link;
- }
/**
- * Perform database queries
- * Enter description here ...
- * @param string $sql
- */
- Public Function query ($sql, $master =true) {
if ($master = = True | | (Substr (Strtolower ($sql), 0,6)! = ' SELECT ') && $master = = False) {
- $this->_res = mysql_query ($sql, $this->_master);
- if (! $this->_res) {
- $this->_error[] = mysql_error ($this->_master);
- }
- $this->_flag = ' master ';
- $this->_link = $this->_master;
- } else {
- $this->_res = mysql_query ($sql, $this->_slave);
- if (! $this->_res) {
- $this->_error[] = mysql_error ($this->_slave);
- }
- $this->_flag = ' slave ';
- $this->_link = $this->_slave;
- }
return $this->_res;
- }
- /**
- * Get single-line records
- * Enter description here ...
- * @param mixed $rs
- */
- Public function Get ($rs = ") {
- if (empty ($rs)) {
- $rs = $this->_res;
- }
- Return mysql_fetch_row ($RS);
- }
- /**
- * Get multiple lines of records
- * Enter description here ...
- * @param mixed $rs
- * @param $result _type
- */
- Public function Fetch ($rs = ") {
- if (empty ($rs)) {
- $rs = $this->_res;
- }
- Return mysql_fetch_array ($rs, MYSQL_ASSOC);
- }
- /**
- * Insert Data
- * Enter description here ...
- * @param unknown_type $sql
- */
- Public function Add ($sql) {
- $rs = $this->query ($sql);
- if ($RS)
- Return mysql_insert_id ($this->_link);
- return false;
- }
- /**
- * Update Data
- * Enter description here ...
- * @param unknown_type $sql
- */
- Public Function Update ($sql) {
- if (empty ($sql)) return false;
- $rs = $this->query ($sql);
- if ($RS)
- return $this->fetchnum ();
- return false;
- }
/**
- * Gets the number of rows affected by the previous statement
- * Enter description here ...
- */
- Public Function Fetchnum () {
- Return Mysql_affected_rows ($this->_link);
- }
- /**
- * destructor to release database connection resources
- * Enter description here ...
- */
- Public Function __destruct () {
- Mysql_close ($this->_link);
- }
- }
Copy Code |