当前位置:首页 > PHP教程 > php类库 > 列表

PHP DB 数据库连接类定义与用法示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-12 10:12:27 浏览: 评论:0 

这篇文章主要介绍了PHP DB 数据库连接类定义与用法,涉及php基于mysqli针对数据库的连接、增删改查等常见操作封装与使用技巧,需要的朋友可以参考下。

本文实例讲述了PHP DB 数据库连接类定义与用法,分享给大家供大家参考,具体如下:

声明:

近期观看了一节 PHP 消息队列视频,对于讲师WiconWang提供的代码,在此分享一下,希望能对爱学习的小伙伴有所帮助…

  1. <?php 
  2. // 数据库连接类 
  3. class DB{ 
  4.  //私有的属性 
  5.  private static $dbcon=false; 
  6.  private $host
  7.  private $port
  8.  private $user
  9.  private $pass
  10.  private $db
  11.  private $charset
  12.  private $link
  13.  //私有的构造方法 
  14.  private function __construct(){ 
  15.   $this->host = 'localhost'
  16.   $this->port = '3306'
  17.   $this->user = 'root'
  18.   $this->pass = 'root'
  19.   $this->db = 'imooc'
  20.   $this->charset= 'utf8'
  21.   //连接数据库 
  22.   $this->db_connect(); 
  23.   //选择数据库 
  24.   $this->db_usedb(); 
  25.   //设置字符集 
  26.   $this->db_charset(); 
  27.   } 
  28.   //连接数据库 
  29.   private function db_connect(){ 
  30.   $this->link=mysqli_connect($this->host.':'.$this->port,$this->user,$this->pass); 
  31.   if(!$this->link){ 
  32.    echo "数据库连接失败<br>"
  33.    echo "错误编码".mysqli_errno($this->link)."<br>"
  34.    echo "错误信息".mysqli_error($this->link)."<br>"
  35.    exit
  36.   } 
  37.   } 
  38.   //设置字符集 
  39.   private function db_charset(){ 
  40.    mysqli_query($this->link,"set names {$this->charset}"); 
  41.   } 
  42.   //选择数据库 
  43.   private function db_usedb(){ 
  44.    mysqli_query($this->link,"use {$this->db}"); 
  45.   } 
  46.   //私有的克隆 
  47.   private function __clone(){ 
  48.    die('clone is not allowed'); 
  49.   } 
  50.   //公用的静态方法 
  51.   public static function getIntance(){ 
  52.    if(self::$dbcon==false){ 
  53.    self::$dbcon=new self; 
  54.    } 
  55.    return self::$dbcon
  56.   } 
  57.   //执行sql语句的方法 
  58.   public function query($sql){ 
  59.    $res=mysqli_query($this->link,$sql); 
  60.    if(!$res){ 
  61.    echo "sql语句执行失败<br>"
  62.    echo "错误编码是".mysqli_errno($this->link)."<br>"
  63.    echo "错误信息是".mysqli_error($this->link)."<br>"
  64.    } 
  65.    return $res
  66.   } 
  67.   //获得最后一条记录id 
  68.   public function getInsertid(){ 
  69.    return mysqli_insert_id($this->link); 
  70.   } 
  71.   /** 
  72.   * 查询某个字段 
  73.   * @param 
  74.   * @return string or int 
  75.   */ 
  76.   public function getOne($sql){ 
  77.    $query=$this->query($sql); 
  78.    return mysqli_free_result($query); 
  79.   } 
  80.   //获取一行记录,return array 一维数组 
  81.   public function getRow($sql,$type="assoc"){ 
  82.    $query=$this->query($sql); 
  83.    if(!in_array($type,array("assoc",'array',"row"))){ 
  84.     die("mysqli_query error"); 
  85.    } 
  86.    $funcname="mysqli_fetch_".$type
  87.    return $funcname($query); 
  88.   } 
  89.   //获取一条记录,前置条件通过资源获取一条记录 
  90.   public function getFormSource($query,$type="assoc"){ 
  91.   if(!in_array($type,array("assoc","array","row"))) 
  92.   { 
  93.    die("mysqli_query error"); 
  94.   } 
  95.   $funcname="mysqli_fetch_".$type
  96.   return $funcname($query); 
  97.   } 
  98.   //获取多条数据,二维数组 
  99.   public function getAll($sql){ 
  100.    $query=$this->query($sql); 
  101.    $list=array(); 
  102.    while ($r=$this->getFormSource($query)) { 
  103.    $list[]=$r
  104.    } 
  105.    return $list
  106.   } 
  107.   public function selectAll($table,$where,$fields='*',$order='',$skip=0,$limit=1000) 
  108.   { 
  109.        if(is_array($where)){ 
  110.           foreach ($where as $key => $val) { 
  111.             if (is_numeric($val)) { 
  112.               $condition = $key.'='.$val
  113.             }else
  114.               $condition = $key.'=\"'.$val.'\"'
  115.             } 
  116.           } 
  117.        } else { 
  118.         $condition = $where
  119.        } 
  120.        if (!emptyempty($order)) { 
  121.          $order = " order by ".$order
  122.        } 
  123.        $sql = "select $fields from $table where $condition $order limit $skip,$limit"
  124.        $query = $this->query($sql); 
  125.        $list = array(); 
  126.        while ($r$this->getFormSource($query)) { 
  127.          $list[] = $r
  128.        } 
  129.        return $list
  130.   } 
  131.    /** 
  132.    * 定义添加数据的方法 
  133.    * @param string $table 表名 
  134.    * @param string orarray $data [数据] 
  135.    * @return int 最新添加的id 
  136.    */ 
  137.    public function insert($table,$data){ 
  138.    //遍历数组,得到每一个字段和字段的值 
  139.    $key_str=''
  140.    $v_str=''
  141.    foreach($data as $key=>$v){ 
  142.    // if(empty($v)){ 
  143.    //  die("error"); 
  144.    // } 
  145.     //$key的值是每一个字段s一个字段所对应的值 
  146.     $key_str.=$key.','
  147.     $v_str.="'$v',"
  148.    } 
  149.    $key_str=trim($key_str,','); 
  150.    $v_str=trim($v_str,','); 
  151.    //判断数据是否为空 
  152.    $sql="insert into $table ($key_str) values ($v_str)"
  153.    $this->query($sql); 
  154.   //返回上一次增加操做产生ID值 
  155.    return $this->getInsertid(); 
  156.   } 
  157.   /* 
  158.   * 删除一条数据方法 
  159.   * @param1 $table, $where=array('id'=>'1') 表名 条件 
  160.   * @return 受影响的行数 
  161.   */ 
  162.   public function deleteOne($table$where){ 
  163.    if(is_array($where)){ 
  164.     foreach ($where as $key => $val) { 
  165.      $condition = $key.'='.$val
  166.     } 
  167.    } else { 
  168.     $condition = $where
  169.    } 
  170.    $sql = "delete from $table where $condition"
  171.    $this->query($sql); 
  172.    //返回受影响的行数 
  173.    return mysqli_affected_rows($this->link); 
  174.   } 
  175.   /* 
  176.   * 删除多条数据方法 
  177.   * @param1 $table, $where 表名 条件 
  178.   * @return 受影响的行数 
  179.   */ 
  180.   public function deleteAll($table$where){ 
  181.    if(is_array($where)){ 
  182.     foreach ($where as $key => $val) { 
  183.      if(is_array($val)){ 
  184.       $condition = $key.' in ('.implode(','$val) .')'
  185.      } else { 
  186.       $condition = $key'=' .$val
  187.      } 
  188.     } 
  189.    } else { 
  190.     $condition = $where
  191.    } 
  192.    $sql = "delete from $table where $condition"
  193.    $this->query($sql); 
  194.    //返回受影响的行数 
  195.    return mysqli_affected_rows($this->link); 
  196.   } 
  197.   /** 
  198.   * [修改操作description] 
  199.   * @param [type] $table [表名] 
  200.   * @param [type] $data [数据] 
  201.   * @param [type] $where [条件] 
  202.   * @return [type] 
  203.   */ 
  204.   public function update($table,$data,$where,$limit=0){ 
  205.    //遍历数组,得到每一个字段和字段的值 
  206.    $str=''
  207.   foreach($data as $key=>$v){ 
  208.    $str.="$key='$v',"
  209.   } 
  210.   $str=rtrim($str,','); 
  211.    if(is_array($where)){ 
  212.     foreach ($where as $key => $val) { 
  213.      if(is_array($val)){ 
  214.       $condition = $key.' in ('.implode(','$val) .')'
  215.      } else { 
  216.       $condition = $key'=' .$val
  217.      } 
  218.     } 
  219.    } else { 
  220.     $condition = $where
  221.    } 
  222.     if (!emptyempty($limit)) { 
  223.       $limit = " limit ".$limit
  224.     }else
  225.       $limit=''
  226.     } 
  227.   //修改SQL语句 
  228.   $sql="update $table set $str where $condition $limit"
  229.   $this->query($sql); 
  230.   //返回受影响的行数 
  231.   return mysqli_affected_rows($this->link); 
  232.   } 
  233. ?> 

使用方法

对DB类中__construct()中的配置信息,进行符合自己数据库的修改

include 引入DB类

使用DB类中的方法需要先进行实例化,以插入数据为例:

  1. $db = DB::getIntance(); 
  2. $insert_data = ['order_id'=>'10010','order_amount' = '200.00','status'=> 1]; 
  3. $res = $db->insert('order_info',$insert_data);

Tags: PHP数据库连接类

分享到:

相关文章