php单例模式应用于mysql数据库操作

PHP中有很多的数据库操作,使用单例模式进行数据库的操作,可以避免大量的new操作浪费资源;

使用单例模式可以很方便的实现一个类来全局控制某些配置信息;


单例模式的要点有三个:

一是某个类只能有一个实例;
二是它必须自行创建这个实例;
三是它必须自行向整个系统提供这个实例。


下里面来一个数据库操作的单例模式实例:

class db{
	public $conn;
	public static $sql;
	public static $intance = null;
	
	protected $host;
	protected $user;
	protected $pass;
	protected $database;
	protected $charset;
	
	public function __construct($host, $user, $pass, $database, $charset){
		$this->host = $host;
		$this->user = $user;
		$this->pass = $pass;
		$this->database = $database;
		$this->charset = $charset;
		
		$this->conn = mysql_connect($this->host, $this->user, $this->pass);
		if(!mysql_select_db($this->database, $this->conn)){
			die('mysql connect failed');
		}
		mysql_query('set name '.$this->charset, $this->conn);
	}
	
	public static function getIntance(){
		if(is_null(self::$intance)){
			self::$intance = new db($this->host, $this->user, $this->pass, $this->database, $this->charset);
		}
		return self::$intance;
	}
	
	/**
	 *@param string $table
	 *@param array $condition 
	 *@param array $field
	 *@return array 
	 */
	public function getRows($table, $condition=array(), $field=array()){
		$where = '';
		if(!empty($condition)){
			foreach ($condition as $k=>$v){
				$where .= $k."='".$v."' and";
			}
			$where = 'where '.$where.' 1=1';
		}
		
		$fieldStr = '';
		if(!empty($field)){
			foreach ($field as $k=>$v){
				$fieldStr .= $v.',';
			}
			$fieldStr = rtrim($fieldStr, ',');
		}else{
			$fieldStr = "*";
		}
		
		self::$sql = "select {$fieldStr} from {$table} {$where}";
		
		$result = mysql_query(self::$sql, $this->conn);
		$resultRow = array();
		$i = 0;
		while($row = mysql_fetch_assoc($result)){
			foreach ($row as $k=>$v){
				$resultRow[$i][$k] = $v;
			}
			$i++;
		}
		
		return $resultRow;
	}
	
	/**
	 * @param string $table
	 * @param array $data
	 * @param array $condition
	 * @return boolean
	 */
	public function update($table, $data=array(), $condition=array()){
		$where = '';
		if(!empty($condition)){
			foreach ($condition as $key=>$val){
				$where .= $key."='".$val."' and ";
			}
			$where = 'where '.$where .' 1=1';
		}
		
		$updateStr = '';
		if(!empty($data)){
			foreach ($data as $key=>$val){
				$updateStr .= $key." = '".$val."' ,";
			}
			$updateStr = 'set '.rtrim($updateStr, ',');			
		}
		
		self::$sql = "update {$table} {$updateStr} {$where}";
		
		return mysql_query(self::$sql, $this->conn);
	}
	
	/**
	 * 
	 * @param string $table
	 * @param array $data
	 * @return number|boolean
	 */
	public function insert($table, $data=array()){
		$fields = '';
		$value = '';
		if(!empty($data)){
			foreach ($data as $k=>$v){
				$fields .= $k.',';
				$value .= "'$v'".',';
			}
			$fields = rtrim($fields, ',');
			$value = rtrim($value, ',');
		}else{
			die($data.' is not allowed empty!');
		}
		self::$sql = "insert into {$table} ({$fields}) values({$value})";
		if(mysql_query(self::$sql, $this->conn)){
			return mysql_insert_id();
		}else{
			return false;
		}
	}
	
	/**
	 * 
	 * @param string $table
	 * @param array $condition
	 * @return resource
	 */
	public function delete($table, $condition=array()){
		$where = '';
		if(!empty($condition)){
			foreach ($condition as $k=>$v){
				$where .= $k." = '".$v."', ";
			}
			$where = " where ".$where." 1=1";
		}
		
		self::$sql = "delete from {$table} {$where}";
		
		return mysql_query(self::$sql, $this->conn);
	}
	
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值