PHP实现基于mysqli的Model基类完整实例,phpmysqlimodel基
PHP实现基于mysqli的Model基类完整实例,phpmysqlimodel基
本文实例讲述了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基于单例模式实现的mysql类
- php封装的连接Mysql类及用法分析
- 一个php Mysql类 可以参考学习熟悉下
- 十二个常见的PHP+MySql类免费CMS系统
- PHP使用Mysqli类库实现完美分页效果的方法
- PHP格式化MYSQL返回float类型的方法
- php实现Mysql简易操作类
- php简单操作mysql数据库的类
- PHP实现的通过参数生成MYSQL语句类完整实例

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
