韦德国际_韦德国际1946官方网站_韦德国际1946手机版
做最好的网站

PHP用单例形式实现1个数额库类,PHP基于单例形式

日期:2019-05-22编辑作者:韦德国际1946手机版

和谐集会演习写的数据库操作基类,包括最基本的CUTiggoD操作.可集成到框架内.

正文实例讲述了PHP基于单例情势达成的数据库操作基类。分享给大家供大家参照他事他说加以考察,具体如下:

运用单例形式的落脚点:  

PHP用单例形式实现1个数额库类,PHP基于单例形式完成的数据库操作基类。确立数据库访问类的包装<?php``class DBDA``{````public $host = ``"localhost"``; ``//服务器地址````public $uid = ``"root"``; ``//数据库的用户名````public $pwd = ``""``; ``//数据库的密码    public $dbname = ``""``;//数据库名``````//执行SQL语句,返回相应结果的函数````//$sql是要执行的SQL语句````//$type是SQL语句的类型,0代表增删改,1代表查询````//$db代表要操作的数据库````public function Query(``$sql``,``$type``=1,``$db``=``"xm_youxiang"``)````{````//造连接对象````$conn = ``new MySQLi(``$this``->host,``$this``->uid,``$this``->pwd,``$db``);``````//判断连接是否成功````!mysqli_connect_error() ``or die``(``"连接失败!"``);``````//执行SQL语句````$result = ``$conn``->query(``$sql``);``````//判断SQL语句类型````if``(``$type``==1)````{````//如果是查询语句返回结果集的二维数组````return $result``->fetch_all();````}````else````{````//如果是其他语句,返回true或false````return $result``;````}````}单例形式连接数据库封装类

数据库操作基类

布局文件:

  壹、php的应用关键在于数据库应用, 所以3个行使中会存在大量的数据库操作, 使用单例格局, 则可避防止大批量的new 操作消耗的能源。

单例形式有叁大规范:

// 配置文件
<?php
$db = array(
        'host'=>'localhost',
        'user'=>'root',
        'password'=>'',
        'database'=>'test',
)

?>
//php 类
<?php
class db {
    public $conn;
    public static $sql;
    public static $instance=null;
    private function __construct(){
        require_once('db.config.php');
        $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
        if(!mysql_select_db($db['database'],$this->conn)){
            echo "失败";
        };
        mysql_query('set names utf8',$this->conn);      
    }
    public static function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new db;
        }
        return self::$instance;
    }
    /**
     * 查询数据库
     */
    public function select($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);
        $resuleRow = array();
        $i = 0;
        while($row=mysql_fetch_assoc($result)){
            foreach($row as $k=>$v){
                $resuleRow[$i][$k] = $v;
            }
            $i  ;
        }
        return $resuleRow;
    }
    /**
     * 添加一条记录
     */
     public function insert($table,$data){
        $values = '';
        $datas = '';
        foreach($data as $k=>$v){
            $values.=$k.',';
            $datas.="'$v'".',';
        }
        $values = rtrim($values,',');
        $datas   = rtrim($datas,',');
        self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
        if(mysql_query(self::$sql)){
            return mysql_insert_id();
        }else{
            return false;
        };
     }
     /**
      * 修改一条记录
      */
    public function update($table,$data,$condition=array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $updatastr = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                $updatastr.= $k."='".$v."',";
            }
            $updatastr = 'set '.rtrim($updatastr,',');
        }
        self::$sql = "update {$table} {$updatastr} {$where}";
        return mysql_query(self::$sql);
    }
    /**
     * 删除记录
     */
     public function delete($table,$condition){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        self::$sql = "delete from {$table} {$where}";
        return mysql_query(self::$sql);

     }

    public static function getLastSql(){
        echo self::$sql;
    }

}
<?php
$db = array(
    'host'=>'localhost',
    'user'=>'root',
    'password'=>'',
    'database'=>'test',
)
?>

  2、倘使系统中须求有一个类来全局调控有些配置新闻, 那么使用单例方式可以很便宜的达成. 那么些能够仿照效法ZF的FrontController部分。

构造函数无法为public

数据库操作基类 ~ 114B    下载(67)

php 数据库基类:

  3、在三次页面请求中, 便于进行调养, 因为具有的代码(比方数据库操作类db)都聚集在三个类中, 大家得以在类中设置钩子, 输出日志,从而幸免各市var_dump, echo。

有多个保存实例的静态成员变量

<?php
$db = array(
        'host'=>'localhost',
        'user'=>'root',
        'password'=>'',
        'database'=>'test',
)

?>
<?php
class db {
  public $conn;
  public static $sql;
  public static $instance=null;
  private function __construct(){
    require_once('db.config.php');
    $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
    if(!mysql_select_db($db['database'],$this->conn)){
      echo "失败";
    };
    mysql_query('set names utf8',$this->conn);
  }
  public static function getInstance(){
    if(is_null(self::$instance)){
      self::$instance = new db;
    }
    return self::$instance;
  }
  /**
   * 查询数据库
   */
  public function select($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);
    $resuleRow = array();
    $i = 0;
    while($row=mysql_fetch_assoc($result)){
      foreach($row as $k=>$v){
        $resuleRow[$i][$k] = $v;
      }
      $i  ;
    }
    return $resuleRow;
  }
  /**
   * 添加一条记录
   */
   public function insert($table,$data){
    $values = '';
    $datas = '';
    foreach($data as $k=>$v){
      $values.=$k.',';
      $datas.="'$v'".',';
    }
    $values = rtrim($values,',');
    $datas  = rtrim($datas,',');
    self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";
    if(mysql_query(self::$sql)){
      return mysql_insert_id();
    }else{
      return false;
    };
   }
   /**
   * 修改一条记录
   */
  public function update($table,$data,$condition=array()){
    $where='';
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."='".$v."' and ";
      }
      $where='where '.$where .'1=1';
    }
    $updatastr = '';
    if(!empty($data)){
      foreach($data as $k=>$v){
        $updatastr.= $k."='".$v."',";
      }
      $updatastr = 'set '.rtrim($updatastr,',');
    }
    self::$sql = "update {$table} {$updatastr} {$where}";
    return mysql_query(self::$sql);
  }
  /**
   * 删除记录
   */
   public function delete($table,$condition){
    $where='';
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."='".$v."' and ";
      }
      $where='where '.$where .'1=1';
    }
    self::$sql = "delete from {$table} {$where}";
    return mysql_query(self::$sql);
   }
  public static function getLastSql(){
    echo self::$sql;
  }
}
$db = db::getInstance();
//$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
//echo $db->insert('demo',array('name'=>'脚本之家','password'=>'123'));
//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
echo $db->delete('demo',array('id'=>'2'));
db::getLastSql();
echo "<pre>";
?>

创立单例注意:

享有访问那几个实例的集体静态方法

数据库操作基类 ~ 3KB    下载(77)

越来越多关于PHP操作数据库相关内容感兴趣的读者可查看本站专项论题:《php mysql数据库操作入门教程》、《PHP基于pdo操作数据库本领总括》及《php常见数据库操作手艺汇总》

  一、1个雷只好有三个类对象(只可以实例化两个对象)

基于单例形式的PHP数据库连接类:

<?php
class db {
    public $conn;
    public static $sql;
    public static $instance=null;
    private function __construct(){
        require_once('db.config.php');
        $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);
        if(!mysql_select_db($db['database'],$this->conn)){
            echo "失败";
        };
        mysql_query('set names utf8',$this->conn);      
    }
    public static function getInstance(){
        if(is_null(self::$instance)){
            self::$instance = new db;
        }
        return self::$instance;
    }
    /**
     * 查询数据库
     */
    public function select($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);
        $resuleRow = array();
        $i = 0;
        while($row=mysql_fetch_assoc($result)){
            foreach($row as $k=>$v){
                $resuleRow[$i][$k] = $v;
            }
            $i  ;
        }
        return $resuleRow;
    }
    /**
     * 添加一条记录
     */
     public function insert($table,$data){
        $values = '';
        $datas = '';
        foreach($data as $k=>$v){
            $values.=$k.',';
            $datas.="'$v'".',';
        }
        $values = rtrim($values,',');
        $datas   = rtrim($datas,',');
        self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
        if(mysql_query(self::$sql)){
            return mysql_insert_id();
        }else{
            return false;
        };
     }
     /**
      * 修改一条记录
      */
    public function update($table,$data,$condition=array()){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        $updatastr = '';
        if(!empty($data)){
            foreach($data as $k=>$v){
                $updatastr.= $k."='".$v."',";
            }
            $updatastr = 'set '.rtrim($updatastr,',');
        }
        self::$sql = "update {$table} {$updatastr} {$where}";
        return mysql_query(self::$sql);
    }
    /**
     * 删除记录
     */
     public function delete($table,$condition){
        $where='';
        if(!empty($condition)){

            foreach($condition as $k=>$v){
                $where.=$k."='".$v."' and ";
            }
            $where='where '.$where .'1=1';
        }
        self::$sql = "delete from {$table} {$where}";
        return mysql_query(self::$sql);

     }

    public static function getLastSql(){
        echo self::$sql;
    }

}

$db = db::getInstance();
//$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));
//echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));
//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));
echo $db->delete('demo',array('id'=>'2'));
db::getLastSql();
echo "<pre>";
?>

愿意本文所述对我们PHP程序设计具有援助。

  2、它必须协和成立这些实例

**class DB{
static private $a;  //实例变量
static private $b;  //
private $dbquery = array(
'host' => 'localhost',
'uid' => 'root',
'password' => '',
'dbname' =>'数据库名',
);
private function __construct(){

本文由韦德国际发布于韦德国际1946手机版,转载请注明出处:PHP用单例形式实现1个数额库类,PHP基于单例形式

关键词: PHP mysql 连接数据库 模式 操作