PHP中基於mysqli實現Model基類的方法及執行個體

來源:互聯網
上載者:User
本篇文章主要介紹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;     }  }?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.