PHP实现基于mysqli的Model基类完整实例

yipeiwu_com5年前Mysql基础

本文实例讲述了PHP实现基于mysqli的Model基类。分享给大家供大家参考,具体如下:

DB.class.php

<?php
  //数据库连接类
  class DB {
     //获取对象句柄
     static public function getDB() {
       $_mysqli = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
       if (mysqli_connect_errno()) {
          echo '数据库连接错误!错误代码:'.mysqli_connect_error();
          exit();
       }
       $_mysqli->set_charset('utf8');
       return $_mysqli;
     }
     //清理,释放资源
     static public function unDB(&$_result, &$_db) {
       if (is_object($_result)) {
          $_result->free();
          $_result = null;
       }
       if (is_object($_db)) {
          $_db->close();
          $_db = null;
       }
     }
  }
?>

Model.class.php

<?php
  //模型基类
  class Model {
     //执行多条SQL语句
     public function multi($_sql) {
       $_db = DB::getDB();
       $_db->multi_query($_sql);
       DB::unDB($_result = null, $_db);
       return true;
     }
     //获取下一个增值id模型
     public function nextid($_table) {
       $_sql = "SHOW TABLE STATUS LIKE '$_table'";
       $_object = $this->one($_sql);
       return $_object->Auto_increment;
     }
     //查找总记录模型
     protected function total($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_total = $_result->fetch_row();
       DB::unDB($_result, $_db);
       return $_total[0];
     }
     //查找单个数据模型
     protected function one($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_objects = $_result->fetch_object();
       DB::unDB($_result, $_db);
       return Tool::htmlString($_objects);
     }
     //查找多个数据模型
    protected function all($_sql) {
       $_db = DB::getDB();
       $_result = $_db->query($_sql);
       $_html = array();
       while (!!$_objects = $_result->fetch_object()) {
          $_html[] = $_objects;
       }
       DB::unDB($_result, $_db);
       return Tool::htmlString($_html);
     }
     //增删修模型
     protected function aud($_sql) {
       $_db = DB::getDB();
       $_db->query($_sql);
       $_affected_rows = $_db->affected_rows;
       DB::unDB($_result = null, $_db);
       return $_affected_rows;
     }
  }
?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

相关文章

php提示Warning:mysql_fetch_array() expects的解决方法

本文实例讲述了php提示Warning mysql_fetch_array() expects的解决方法,分享给大家供大家参考。具体分析如下: 在mysql数据库连接时碰到Warning...

在mysql数据库原有字段后增加新内容

复制代码 代码如下:update table set user=concat(user,$user) where xx=xxx;...

IIS下配置Php+Mysql+zend的图文教程

IIS下配置Php+Mysql+zend的图文教程

  为了迎接dvphp公测,特发此教程,希望对一些不会的朋友有所帮助,希望会的朋友多多指教!   下面的教程都是在windows 2000下实现的,其他系统请自己参...

使用PHPMYADMIN操作mysql数据库添加新用户和数据库的方法

使用PHPMYADMIN操作mysql数据库添加新用户和数据库的方法

1、新建数据库:  进入phpmyadmin管理界面以后,找到如下文字“创建一个新的数据库”,然后在下面填上要新建的数据库的名称,在后面的下拉列表中选择数据库的编码,如“utf8_gen...

Mysql和网页显示乱码解决方法集锦

将数据插入到php?name=%CA%FD%BE%DD%BF%E2" onclick="tagshow(event)" class="t_tag">数据库的时候...