單例模式數據庫連接


 

單例基礎知識簡介+案例

 

連接數據庫時使用單例模式,避免造成對數據庫反復連接造成的浪費!

主要分兩部分

    第一部分:數據庫連接的單例

    第二部分:DB類的調用

 

1.數據庫的連接

 

<?php

    class Database
    {
        private $pdo;
        static $instance;
        private function __construct()
        {
       //此處用的是常量,可替換中自己對應的數據庫
            $this->pdo = new PDO(DSN,USER,PWD);
        }
        private function __clone()
        {
        }
        public static function getInstance()
        {
            if(!(self::$instance instanceof self)){
                self::$instance = new self();
            }
            return self::$instance;
        }
        public function getPDO()
        {
            return $this->pdo;
        }
    }
    $database = Database::getInstance();
    $pdo = $database->getPDO();


 ?>

 

 

 

2.DB類調用連接

<?php


	// 數據庫類,其他類都可用
	class DB
	{
		// pdo對象
		private $pdo;

		// 字段名
		private $field = '*';

		// 表名
		private $table;

		// 條件
		private $where;

		// 分組
		private $group;

		// 篩選
		private $having;

		// 排序
		private $order;

		// 分頁
		private $limit;

		// sql源生語句
		public $sql;


		// 鏈接數據庫
		public function __construct()
		{
			// $this->pdo = new PDO(DSN, USER, PWD);
			$database = Database::getInstance();
			$this->pdo = $database->getPDO();
		}


		// 查詢數據
		public function select()
		{
			$this->sql = 'select '.$this->field.' from '.$this->table.$this->where.$this->group.$this->having.$this->order.$this->limit;


			$this->init();

			$res = $this->pdo->query($this->sql);

			if(is_object($res)){
				$data = $res->fetchAll(PDO::FETCH_ASSOC);
				return $data;
			}

			return false;
		}

		// 單數據查詢
		public function find()
		{
			$this->sql = ' select '.$this->field.' from '.$this->table.$this->where;
			$this->init();

			$res = $this->pdo->query($this->sql);

			if(is_object($res)){
				$data = $res->fetch(PDO::FETCH_ASSOC);
				return $data;
			}

			return false;
		}

		// 刪除數據
		public function delete()
		{
			$this->sql = 'delete from '.$this->table.$this->where.$this->order.$this->limit;
			$this->init();

			$res = $this->pdo->exec($this->sql);

			return $res;
		}

		// 插入數據
		public function insert($data = '')
		{
			if(!is_array($data)){
				return false;
			}

			// 准備sql
				$field = null;
				$value = null;

				// 拼接之前, 保證$data 里的數據 不用再調整
				foreach($data as $k => $v){
					$field .= '`'.$k.'`,';

					$value .= '"'.$v.'",';
				}

				$field = rtrim($field, ',');
				$value = rtrim($value, ',');

				$this->sql = 'insert into '.$this->table.' ( '.$field.' )  values('.$value.')';

			// 執行sql
				$res = $this->pdo->exec($this->sql);

				if($res){
					$newId = $this->pdo->lastInsertId();

					return $newId;
				}else{
					return false;
				}
		}

		// 更新數據
		public function update($data = '')
		{
			if(!is_array($data)){
				return false;
			}

			// 准備sql
				$str = null;

				// 拼接之前, 保證$data 里的數據 不用再調整
				foreach($data as $k => $v){
					$str .= '`'.$k.'`="'.$v.'"'.',';
				}

				$str = rtrim($str, ',');


				$this->sql = 'update '.$this->table.' set '.$str.$this->where;

				$this->init();

			// 執行sql
				$res = $this->pdo->exec($this->sql);
				if($res){
					return $res;
				}else{
					return false;
				}
		}


		public function field( $param  = '')
		{
			if( $param == '' ){
				$this->field = '*';
			}else{
				$this->field = $param;
			}

			return $this;
		}

		public function table($param = '')
		{
			$this->table = $param;

			return $this;
		}

		public function where($param = '')
		{
			if($param == ''){
				$this->where = null;
			}else{
				$this->where = ' where '.$param;
			}

			return $this;
		}

		public function group($param = '')
		{
			if($param == ''){
				$this->group = null;
			}else{
				$this->group = ' group by '.$param;
			}

			return $this;
		}

		public function having($param = '')
		{
			if($param == ''){
				$this->having = null;
			}else{
				$this->having = ' having '.$param;
			}

			return $this;
		}

		public function order($param = '')
		{
			if($param == ''){
				$this->order = null;
			}else{
				$this->order = ' order by '.$param;
			}

			return $this;
		}

		public function limit($param = '')
		{
			if($param == ''){
				$this->limit = null;
			}else{
				$this->limit = ' limit '.$param;
			}

			return $this;
		}

		// 初始化所有字段
		public function init()
		{
			$this->field = '*';
			$this->table = null;
			$this->where = null;
			$this->group = null;
			$this->having = null;
			$this->order = null;
			$this->limit = null;
		}
	}






 ?>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM