栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

PHP模型Model类封装数据库操作示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP模型Model类封装数据库操作示例

本文实例讲述了PHP模型Model类封装数据库操作。分享给大家供大家参考,具体如下:

tableName = PRE.$tableName;
      //2.初始化连接数据库
      $this->getConnect();
      //3.获得当前表的所有字段
      $this->getFields();
    }
    
    public function getConnect()
    {
      //1.连接数据库
      $this->link = mysqli_connect(HOST,USER,PWD,DB,PORT);
      //2.判断连接
      if (mysqli_connect_errno($this->link)>0){
 echo mysqli_connect_error($this->link);
 exit;
      }
    }
    
    public function query($sql)
    {
      $result = mysqli_query($this->link,$sql);
      if ($result && mysqli_num_rows($result)>0) {
$arr = [];
while($row = mysqli_fetch_assoc($result)){
   $arr[] = $row;
}
      }
    return $arr;
    }
    
    public function getFields()
    {
      //查看表结构
      $sql = "desc {$this->tableName}";
      //执行并发送SQL
      $result = $this->query($sql);
      $fields = [];
      foreach ($result as $k => $v){
 $fields[] = $v['Field'];
      }
      $this->allFields = $fields;
    }
    
    public function exec($sql)
    {
      $result = mysqli_query($this->link,$sql);
      //处理结果集
      if ($result && mysqli_affected_rows($this->link)>0){
 //判断是否为添加操作,是则返回上一次执行的id
 if (mysqli_insert_id($this->link)){
   return mysqli_insert_id($this->link);
 }
 //删除修改操作成功则返回true
 return true;
      }else{
 //未执行成功则返回false
 return false;
      }
    }
    
    public function select()
    {
      $sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
      //执行并发送SQL
      return $this->query($sql);
    }
    
    public function find($id="")
    {
      //判断id是否存在
      if (empty($id)){
 $where = $this->where;
      }else{
 $where = "where id={$id}";
      }
      $sql = "select {$this->field} from {$this->tableName} {$where} limit 1";
      //执行并发送sql
      $result = $this->query($sql);
      //返回一条数据
      return $result[0];
    }
    
    public function field($field)
    {
      //判断字段是否存在
      if (empty($field)){
 return $this;
      }
      $this->field = $field;
      return $this;
    }
    
    public function count()
    {
      //准备SQL语句
      $sql = "select count(*) as total from {$this->tableName} limit 1";
      $result = $this->query($sql);
      //返回总数
      return $result[0]['total'];
    }
    
    public function add($data){
      //判断是否是数组
      if (!is_array($data)){
 return $this;
      }
      //判断是否全是非法字段
      if (empty($data)){
 die("非法数据");
      }
      //过滤非法字段
      foreach ($data as $k => $v){
 if (!in_array($k,$this->allFields)){
   unset($data[$k]);
 }
      }
      //将数组中的键取出
      $keys = array_keys($data);
      //将数组中取出的键转为字符串拼接
      $key = implode(",",$keys);
      //将数组中的值转化为字符串拼接
      $value = implode("','",$data);
      //准备SQL语句
      $sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
      //执行并发送SQL
      return $this->exec($sql);
    }
    
    public function delete($id="")
    {
      //判断id是否存在
      if (empty($id)){
 $where = $this->where;
      }else{
 $where = "where id={$id}";
      }
      $sql = "delete from {$this->tableName} {$where}";
      echo $sql;
      //执行并发送
      return $this->exec($sql);
    }
    
    public function update($data){
      //判断是否是数组
      if (!is_array($data)){
 return $this;
      }
      //判断是否是全是非法字段
      if(empty($data)){
 die("非法数据");
      }
      $str = "";
      //过滤非法字段
      foreach ($data as $k => $v){
 if ($k == "id"){
   $where = "where id={$v}";
   unset($data[$k]);
 }
 if (in_array($k,$this->allFields)){
   $str .= "{$k}='{$v}',";
 }else{
   unset($data[$k]);
 }
      }
      //判断是否有条件
      if (empty($this->where)){
 die("请输入条件");
      }
      //去掉最右侧的逗号
      $str = rtrim($str,",");
      $sql = "update {$this->tableName} set {$str} {$this->where}";
      return $this->exec($sql);
    }
    
    public function where($where)
    {
      $this->where = "where ".$where;
      return $this;
    }
    
    public function order($order)
    {
      $this->order = "order by ".$order;
      return $this;
    }
    
    public function limit($limit)
    {
      $this->limit = "limit ".$limit;
      return $this;
    }
    
    public function __destruct()
    {
      mysqli_close($this->link);
    }
  }
 //自行调试
 $a = new Model("表名");
 // var_dump($a->find(3));
 // var_dump($a->select());
  // var_dump($a->count());
  // $res = $a->select();
  //var_dump($res);
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/39320.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号