`
tw5566
  • 浏览: 449228 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

开源框架canphp中mysql与mysqli的连接类

    博客分类:
  • php
 
阅读更多
<?php
class cpMysqli {
	private $_writeLink = NULL; //主
	private $_readLink = NULL; //从
	private $_replication = false; //标志是否支持主从
	private $dbConfig = array();
	public $sql = "";
	
	public function __construct($dbConfig = array()){
		$this->dbConfig = $dbConfig;
		//判断是否支持主从				
		$this->_replication = isset($this->dbConfig['DB_SLAVE']) && !empty($this->dbConfig['DB_SLAVE']);
	}
	
	//执行sql查询	
	public function query($sql, $params = array()) {
		foreach($params as $k => $v){
			$sql = str_replace(':'.$k, $this->escape($v), $sql);
		}
		$this->sql = $sql;
		if( $query = $this->_getReadLink()->query($sql) )
			return $query;
		else
			$this->error('MySQL Query Error', $this->_getReadLink()->error, $this->_getReadLink()->errno);
	}
	
	//执行sql命令
	public function execute($sql, $params = array()) {
		foreach($params as $k => $v){
			$sql = str_replace(':'.$k, $this->escape($v), $sql);
		}
		$this->sql = $sql;
		if( $query = $this->_getWriteLink()->query($sql) )
			return $query;
		else
			$this->error('MySQL Query Error', $this->_getWriteLink()->error, $this->_getWriteLink()->errno);
	}
	
	//从结果集中取得一行作为关联数组,或数字数组,或二者兼有 
	public function fetchArray($query, $result_type = MYSQLI_ASSOC) {
		return $this->unEscape( $query->fetch_array($result_type) );
	}	
	
	//取得前一次 MySQL 操作所影响的记录行数
	public function affectedRows() {
		return $this->_getWriteLink()->affected_rows;
	}
	
	//获取上一次插入的id
	public function lastId() {
		return $this->_getWriteLink()->insert_id;
	}
	
	//获取表结构
	public function getFields($table) {
		$this->sql = "SHOW FULL FIELDS FROM {$table}";
		$query = $this->query($this->sql);
		$data = array();
		while($row = $this->fetchArray($query)){
			$data[] = $row;
		}
		return $data;
	}
	
	//获取行数
	public function count($table,$where) {
		$this->sql = "SELECT count(*) FROM $table $where";
		$query = $this->query($this->sql);
        $data = $this->fetchArray($query);
		return $data['count(*)'];
	}
	
	//数据过滤
	public function escape($value) {
		if( isset($this->_readLink) ) {
            $mysqli = $this->_readLink;
        } elseif( isset($this->_writeLink) ) {
            $mysqli = $this->_writeLink;
        } else {
            $mysqli = $this->_getReadLink();
        }

		if( is_array($value) ) { 
		   return array_map(array($this, 'escape'), $value);
		} else {
		   if( get_magic_quotes_gpc() ) {
			   $value = stripslashes($value);
		   } 
		   return "'" . $mysqli->real_escape_string($value) . "'";
		}
	}
	
	//数据过滤
	public function unEscape($value) {
		if (is_array($value)) {
			return array_map('stripslashes', $value);
		} else {
			return stripslashes($value);
		}
	}	
	
	//解析待添加或修改的数据
	public function parseData($options, $type) {
		//如果数据是字符串,直接返回
		if(is_string($options['data'])) {
			return $options['data'];
		}
		if( is_array($options) && !empty($options) ) {
			switch($type){
				case 'add':
						$data = array();
						$data['fields'] = array_keys($options['data']);
						$data['values'] = $this->escape( array_values($options['data']) );
						return " (`" . implode("`,`", $data['fields']) . "`) VALUES (" . implode(",", $data['values']) . ") ";
				case 'save':
						$data = array();
						foreach($options['data'] as $key => $value) {
								$data[] = " `$key` = " . $this->escape($value);
						}
						return implode(',', $data);
			default:return false;
			}
		}
		return false;
	}
	
	//解析查询条件
	public function parseCondition($options) {
		$condition = "";	
		if(!empty($options['where'])) {
			$condition = " WHERE ";
			if(is_string($options['where'])) {
				$condition .= $options['where'];
			} else if(is_array($options['where'])) {
					foreach($options['where'] as $key => $value) {
						 $condition .= " `$key` = " . $this->escape($value) . " AND ";
					}
					$condition = substr($condition, 0,-4);	
			} else {
				$condition = "";
			}
		}
		
		if( !empty($options['group']) && is_string($options['group']) ) {
			$condition .= " GROUP BY " . $options['group'];
		}
		if( !empty($options['having']) && is_string($options['having']) ) {
			$condition .= " HAVING " .  $options['having'];
		}
		if( !empty($options['order']) && is_string($options['order']) ) {
			$condition .= " ORDER BY " .  $options['order'];
		}
		if( !empty($options['limit']) && (is_string($options['limit']) || is_numeric($options['limit'])) ) {
			$condition .= " LIMIT " .  $options['limit'];
		}
		if( empty($condition) ) return "";
        return $condition;
	}

	//输出错误信息
	public function error($message = '',$error = '', $errorno = ''){
		if( DEBUG ){
			$str = " {$message}<br>
					<b>SQL</b>: {$this->sql}<br>
					<b>错误详情</b>: {$error}<br>
					<b>错误代码</b>:{$errorno}<br>"; 
		} else {
			$str = "<b>出错</b>: $message<br>";
		}
		throw new Exception($str);
	}
	
	//获取从服务器连接
    private function _getReadLink() {
        if( isset( $this->_readLink ) ) {
			$this->_readLink->ping();
            return $this->_readLink;
        } else {
            if( !$this->_replication ) {
				return $this->_getWriteLink();
           	} else {
                $this->_readLink = $this->_connect( false );
                return $this->_readLink;
            }
        }
    }
	
	//获取主服务器连接
    private function _getWriteLink() {
        if( isset( $this->_writeLink ) ) {
			$this->_writeLink->ping();
            return $this->_writeLink;
        } else{
            $this->_writeLink = $this->_connect( true );
            return $this->_writeLink;
        }
    }
	
	//数据库链接
	private  function _connect($is_master = true) {
		if( ($is_master == false) && $this->_replication ) {	
			$slave_count = count($this->dbConfig['DB_SLAVE']);
			//遍历所有从机
			for($i = 0; $i < $slave_count; $i++) {
				$db_all[] = array_merge($this->dbConfig, $this->dbConfig['DB_SLAVE'][$i]);
			}
			$db_all[] = $this->dbConfig;//如果所有从机都连接不上,连接到主机
			//随机选择一台从机连接
			$rand =  mt_rand(0, $slave_count-1);
			$db = array_unshift($db_all, $db_all[$rand]);		
		} else {
			$db_all[] = $this->dbConfig; //直接连接到主机
		}

		foreach($db_all as $db) {
			$mysqli = @new mysqli($db['DB_HOST'], $db['DB_USER'], $db['DB_PWD'], $db['DB_NAME'], $db['DB_PORT']);
			if($mysqli->connect_errno == 0 ) {
				break;
			}
		}

		if($mysqli->connect_errno){
			$this->error('无法连接到数据库服务器', $mysqli->connect_error, $mysqli->connect_errno);
		}
		//设置编码
		$mysqli->query("SET NAMES {$db['DB_CHARSET']}");
        return $mysqli;
	}
	
	//关闭数据库
	public function __destruct() {
		if($this->_writeLink) {
			$this->_writeLink->close();
		}
		if($this->_readLink) {
			$this->_readLink->close();
		}
	} 
}

 

<?php
class cpMysql {
	private $_writeLink = NULL; //主
	private $_readLink = NULL; //从
	private $_replication = false; //标志是否支持主从
	private $dbConfig = array();
	public $sql = "";
	
	public function __construct( $dbConfig = array() ){
		$this->dbConfig = $dbConfig;
		//判断是否支持主从				
		$this->_replication = isset( $this->dbConfig['DB_SLAVE']) && !empty($this->dbConfig['DB_SLAVE'] );
	}
	
	//执行sql查询	
	public function query($sql, $params = array()) {
		foreach($params as $k => $v){
			$sql = str_replace(':'.$k, $this->escape($v), $sql);
		}
		$this->sql = $sql;
		if( $query = mysql_query($sql, $this->_getReadLink()) )
			return $query;
		else
			$this->error('MySQL Query Error');
	}
	
	//执行sql命令
	public function execute($sql, $params = array()) {
		foreach($params as $k => $v){
			$sql = str_replace(':'.$k, $this->escape($v), $sql);
		}
		$this->sql = $sql;
		if( $query = mysql_query($sql, $this->_getWriteLink()) )
			return $query;
		else
			$this->error('MySQL Query Error');
	}
	
	//从结果集中取得一行作为关联数组,或数字数组,或二者兼有 
	public function fetchArray($query, $result_type = MYSQL_ASSOC) {
		return $this->unEscape( mysql_fetch_array($query, $result_type) );
	}	
	
	//取得前一次 MySQL 操作所影响的记录行数
	public function affectedRows() {
		return mysql_affected_rows($this->_getWriteLink());
	}
	
	//获取上一次插入的id
	public function lastId() {
		return ($id = mysql_insert_id( $this->_getWriteLink() )) >= 0 ? $id : mysql_result($this->execute("SELECT last_insert_id()"), 0);
	}
	
	//获取表结构
	public function getFields($table) {
		$this->sql = "SHOW FULL FIELDS FROM {$table}";
		$query = $this->query($this->sql);
		$data = array();
		while($row = $this->fetchArray($query)){
			$data[] = $row;
		}
		return $data;
	}
	
	//获取行数
	public function count($table,$where) {
		$this->sql = "SELECT count(*) FROM $table $where";
		$query = $this->query($this->sql);
        $data = $this->fetchArray($query);
		return $data['count(*)'];
	}
	
	//数据过滤
	public function escape($value) {
		if( isset($this->_readLink) ) {
            $link = $this->_readLink;
        } elseif( isset($this->_writeLink) ) {
            $link = $this->_writeLink;
        } else {
            $link = $this->_getReadLink();
        }

		if( is_array($value) ) { 
		   return array_map(array($this, 'escape'), $value);
		} else {
		   if( get_magic_quotes_gpc() ) {
			   $value = stripslashes($value);
		   } 
			return	"'" . mysql_real_escape_string($value, $link) . "'";
		}
	}
	
	//数据过滤
	public function unEscape($value) {
		if (is_array($value)) {
			return array_map('stripslashes', $value);
		} else {
			return stripslashes($value);
		}
	}	
	
	//解析待添加或修改的数据
	public function parseData($options, $type) {
		//如果数据是字符串,直接返回
		if(is_string($options['data'])) {
			return $options['data'];
		}
		if( is_array($options) && !empty($options) ) {
			switch($type){
				case 'add':
						$data = array();
						$data['fields'] = array_keys($options['data']);
						$data['values'] = $this->escape( array_values($options['data']) );
						return " (`" . implode("`,`", $data['fields']) . "`) VALUES (" . implode(",", $data['values']) . ") ";
				case 'save':
						$data = array();
						foreach($options['data'] as $key => $value) {
								$data[] = " `$key` = " . $this->escape($value);
						}
						return implode(',', $data);
			default:return false;
			}
		}
		return false;
	}
	
	//解析查询条件
	public function parseCondition($options) {
		$condition = "";
		if(!empty($options['where'])) {
			$condition = " WHERE ";
			if(is_string($options['where'])) {
				$condition .= $options['where'];
			} else if(is_array($options['where'])) {
					foreach($options['where'] as $key => $value) {
						 $condition .= " `$key` = " . $this->escape($value) . " AND ";
					}
					$condition = substr($condition, 0,-4);	
			} else {
				$condition = "";
			}
		}
		
		if( !empty($options['group']) && is_string($options['group']) ) {
			$condition .= " GROUP BY " . $options['group'];
		}
		if( !empty($options['having']) && is_string($options['having']) ) {
			$condition .= " HAVING " .  $options['having'];
		}
		if( !empty($options['order']) && is_string($options['order']) ) {
			$condition .= " ORDER BY " .  $options['order'];
		}
		if( !empty($options['limit']) && (is_string($options['limit']) || is_numeric($options['limit'])) ) {
			$condition .= " LIMIT " .  $options['limit'];
		}
		if( empty($condition) ) return "";
        return $condition;
	}
	
	//输出错误信息
	public function error($message = ''){
		$error = mysql_error();
		$errorno = mysql_errno();
		if( DEBUG ){
			$str = " {$message}<br>
					<b>SQL</b>: {$this->sql}<br>
					<b>错误详情</b>: {$error}<br>
					<b>错误代码</b>:{$errorno}<br>"; 
		} else {
			$str = "<b>出错</b>: $message<br>";
		}
		throw new Exception($str);
	}
	
	/******************兼容以前的版本*****************************/
		//选择数据库
	public function select_db($dbname) {
		return mysql_select_db($dbname, $this->_getWriteLink());
	}
	
	//从结果集中取得一行作为关联数组,或数字数组,或二者兼有 
	public function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return $this->fetchArray($query, $result_type);
	}
	//获取上一次插入的id
	public function insert_id() {
		return $this->lastId();
	}
	//取得前一次 MySQL 操作所影响的记录行数
	public function affected_rows() {
		return $this->affectedRows();
	}
	//取得结果集中行的数目
	public function num_rows($query) {
		return mysql_num_rows($query);
	}
	/******************兼容以前的版本*****************************/

	//获取从服务器连接
    private function _getReadLink() {
        if( isset( $this->_readLink ) ) {
			 mysql_ping( $this->_readLink );
            return $this->_readLink;
        } else {
            if( !$this->_replication ) {
				return $this->_getWriteLink();
           	} else {
                $this->_readLink = $this->_connect( false );
                return $this->_readLink;
            }
        }
    }
	
	//获取主服务器连接
    private function _getWriteLink() {
        if( isset( $this->_writeLink ) ) {
			mysql_ping( $this->_writeLink );
            return $this->_writeLink;
        } else{
            $this->_writeLink = $this->_connect( true );
            return $this->_writeLink;
        }
    }
	
	//数据库链接
	private  function _connect($is_master = true) {
		if( ($is_master == false) && $this->_replication ) {	
			$slave_count = count($this->dbConfig['DB_SLAVE']);
			//遍历所有从机
			for($i = 0; $i < $slave_count; $i++) {
				$db_all[] = array_merge($this->dbConfig, $this->dbConfig['DB_SLAVE'][$i]);
			}
			$db_all[] = $this->dbConfig;//如果所有从机都连接不上,连接到主机
			//随机选择一台从机连接
			$rand =  mt_rand(0, $slave_count-1);
			$db = array_unshift($db_all, $db_all[$rand]);			
		} else {
			$db_all[] = $this->dbConfig; //直接连接到主机
		}

		foreach($db_all as $db) {
			if( $link = @mysql_connect($db['DB_HOST'] . ':' . $db['DB_PORT'], $db['DB_USER'], $db['DB_PWD'])) {
				break;
			}
		}
		
		if(!$link){
			$this->error('无法连接到数据库服务器');
		}

		$version = mysql_get_server_info($link);
		if($version > '4.1') {
			mysql_query("SET character_set_connection = " . $db['DB_CHARSET'] . ", character_set_results = " . $db['DB_CHARSET'] . ", character_set_client = binary", $link);		
				
			if($version > '5.0.1') {
				mysql_query("SET sql_mode = ''", $link);
			}
		}		
        mysql_select_db($db['DB_NAME'], $link);
        return $link;
	}
	
	//关闭数据库
	public function __destruct() {
		if($this->_writeLink) {
			@mysql_close($this->_writeLink);
		}
		if($this->_readLink) {
			@mysql_close($this->_readLink);
		}
	} 
}

 

分享到:
评论

相关推荐

    canphp框架 canphp canphp手册

    canphp手册 canphp框架 canphp canphp手册 canphp框架 canphp canphp手册

    canphp框架(php框架) v1.5

    不管您是在做大项目还是小项目或对开源系统二次开发,CanPHP框架都可以帮助您。CanPHP框架 v1.5 升级日志:1、增加xml类,用于xml数据转换成php数组2、增强分页类,优化生成的分页网址,增加长文章内容分页功能3、...

    canphp框架

    canphp框架

    canphp框架-php框架

    CanPHP框架(简称CP),是一个简单、自由、实用、高效的php框架。里面集成了开发文档,以及cms

    CanPHP框架(简称CP)2.0

    CanPHP框架是一个简洁,实用,高效,遵循apache协议的php开源框架,以“简单、自由、包容”为理念,主要是为了简化和快速开发小型项目和开源系统二次开发而诞生。它既可以完美的支持MVC模式,又可以不受限制的支持...

    CanPHP框架(CP) 2.0.rar

    CanPHP框架以面向应用为主,不纠结于OOP,不纠结于MVC,不纠结于设计模式,不拘一格,力求简单快速优质的完成项目开发,是中小型项目开发首选php框架。  CanPHP框架(CP) 2.0 升级日志: 1、重构了数据库层,内置...

    CanPHP1.5开发手册

    CanPHP框架是一个简洁,实用,高效,遵循apache协议的php开源框架,以“简单、自由、包容”为理念,主要是为了简化和快速开发小型项目和开源系统二次开发而诞生。它既可以完美的支持MVC模式,又可以不受限制的支持...

    CanPHP框架开发手册 for v1.5

    答:canphp很简单,很自由,功能也很实用,里面的类库都是独立的,随时都可以提取出来放到自己的其他项目中,即使你把它当作一个框架,你还可以把它当作一个工具包。 6、用什么开发工具去开发canphp比较好?答:php...

    canphp框架(php框架) v1.5.zip

    不管您是在做大项目还是小项目或对开源系统二次开发,CanPHP框架都可以帮助您。 CanPHP框架 v1.5 升级日志: 1、增加xml类,用于xml数据转换成php数组 2、增强分页类,优化生成的分页网址,增加长文章内容分页...

    canphp2.0betaphp框架

    CanPHP框架以面向应用为主,不纠结于OOP,不纠结于MVC,不纠结于设计模式,不拘一格,力求简单快速优质的完成项目开发,是中小型项目开发首选php框架。 CanPHP框架(CP) 2.0 升级日志: 1、重构了数据库层,内置数据...

    canphp v1.0 beta 免费的php框架.rar

    CanPHP框架(简称CP),是一个简单、自由、实用、高效的php框架.里面内置了一个cms。

    canphp:canphp框架

    Canphp canphp框架

    canphp1.5想到即可做到

    canphp1.5想到即可做到

    CanPHP框架开发手册 for v1.5 CHM版.rar

    答:canphp很简单,很自由,功能也很实用,里面的类库都是独立的,随时都可以提取出来放到自己的其他项目中,即使你把它当作一个框架,你还可以把它当作一个工具包。 6、用什么开发工具去开发canphp比较好? 答:...

    canphp 做的企业站后台(适合中小企业站)

    canphp 做的企业站后台(适合中小企业站);前后台代码齐全,包括数据库

    canphp1.4.zip_canphp_php based projects

    CanPHP框架(简称CP),是一个简单、自由、实用、高效的php框架。...CanPHP框架以面向应用为主,不纠结于OOP,不纠结于MVC,不纠结于设计模式,不拘一格,力求简单快速优质的完成项目开发,是中小型项目开发首选php框架。

    基于PHP的canphp2.0betaphp框架源码.zip

    基于PHP的canphp2.0betaphp框架源码.zip

    基于PHP的canphp 2.0 beta php框架.zip

    基于PHP的canphp 2.0 beta php框架.zip

    dlcms开源php网站管理系统v3.0.0.192

    独立网络内容管理系统(DLCMS)是一款以CANPHP框架为核心,PHP 和 MySQL 数据库构建的简洁高效内容管理系统,为中小型网站提供一个完美的解决方案。 1、模块分离 DLCMS为特有的模块式组装网站,任何功能相互不依赖可以...

Global site tag (gtag.js) - Google Analytics