PHP模型Model類(lèi)封裝數(shù)據(jù)庫(kù)操作示例
本文實(shí)例講述了PHP模型Model類(lèi)封裝數(shù)據(jù)庫(kù)操作。分享給大家供大家參考,具體如下:
<?php
//引入配置文件
include "./config.php";
class Model
{
public $link;//存儲(chǔ)連接對(duì)象
public $tableName = "";//存儲(chǔ)表名
public $field = "*";//存儲(chǔ)字段
public $allFields = [];//存儲(chǔ)當(dāng)前表所有字段
public $where = "";//存儲(chǔ)where條件
public $order = "";//存儲(chǔ)order條件
public $limit = "";//存儲(chǔ)limit條件
/**
* 構(gòu)造方法 初始化
* @param string $tableName 要操作的表名
*/
public function __construct($tableName)
{
//1.存儲(chǔ)操作的表名
$this->tableName = PRE.$tableName;
//2.初始化連接數(shù)據(jù)庫(kù)
$this->getConnect();
//3.獲得當(dāng)前表的所有字段
$this->getFields();
}
/**
* 初始化連接數(shù)據(jù)庫(kù)操作
*/
public function getConnect()
{
//1.連接數(shù)據(jù)庫(kù)
$this->link = mysqli_connect(HOST,USER,PWD,DB,PORT);
//2.判斷連接
if (mysqli_connect_errno($this->link)>0){
echo mysqli_connect_error($this->link);
exit;
}
}
/**
* 執(zhí)行并發(fā)送SQL(查詢)
* @param string $sql 要查詢的SQL語(yǔ)句
* @return array 返回查詢出來(lái)的二維數(shù)組
*/
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;
}
/**
* 獲取當(dāng)前表的所有字段
*/
public function getFields()
{
//查看表結(jié)構(gòu)
$sql = "desc {$this->tableName}";
//執(zhí)行并發(fā)送SQL
$result = $this->query($sql);
$fields = [];
foreach ($result as $k => $v){
$fields[] = $v['Field'];
}
$this->allFields = $fields;
}
/**
* 執(zhí)行并發(fā)送SQL語(yǔ)句(增刪改)
* @param string $sql 要執(zhí)行的SQL語(yǔ)句
* @return bool|int|string 添加成功則返回上一次操作id,刪除修改操作則返回true,失敗則返回false
*/
public function exec($sql)
{
$result = mysqli_query($this->link,$sql);
//處理結(jié)果集
if ($result && mysqli_affected_rows($this->link)>0){
//判斷是否為添加操作,是則返回上一次執(zhí)行的id
if (mysqli_insert_id($this->link)){
return mysqli_insert_id($this->link);
}
//刪除修改操作成功則返回true
return true;
}else{
//未執(zhí)行成功則返回false
return false;
}
}
/**
* 查詢多條數(shù)據(jù)
*/
public function select()
{
$sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
//執(zhí)行并發(fā)送SQL
return $this->query($sql);
}
/**
* 查詢一條數(shù)據(jù)
* @param string $id 要查詢的id
* @return array 返回一條數(shù)據(jù)
*/
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";
//執(zhí)行并發(fā)送sql
$result = $this->query($sql);
//返回一條數(shù)據(jù)
return $result[0];
}
/**
* 設(shè)置要查詢的字段信息
* @param string $field 要查詢的字段
* @return object 返回自己,保證連貫操作
*/
public function field($field)
{
//判斷字段是否存在
if (empty($field)){
return $this;
}
$this->field = $field;
return $this;
}
/**
* 統(tǒng)計(jì)總條數(shù)
* @return int 返回總數(shù)
*/
public function count()
{
//準(zhǔn)備SQL語(yǔ)句
$sql = "select count(*) as total from {$this->tableName} limit 1";
$result = $this->query($sql);
//返回總數(shù)
return $result[0]['total'];
}
/**
* 添加操作
* @param array $data 要添加的數(shù)組
* @return bool|int|string 添加成功則返回上一次操作的id,失敗則返回false
*/
public function add($data){
//判斷是否是數(shù)組
if (!is_array($data)){
return $this;
}
//判斷是否全是非法字段
if (empty($data)){
die("非法數(shù)據(jù)");
}
//過(guò)濾非法字段
foreach ($data as $k => $v){
if (!in_array($k,$this->allFields)){
unset($data[$k]);
}
}
//將數(shù)組中的鍵取出
$keys = array_keys($data);
//將數(shù)組中取出的鍵轉(zhuǎn)為字符串拼接
$key = implode(",",$keys);
//將數(shù)組中的值轉(zhuǎn)化為字符串拼接
$value = implode("','",$data);
//準(zhǔn)備SQL語(yǔ)句
$sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
//執(zhí)行并發(fā)送SQL
return $this->exec($sql);
}
/**
* 刪除操作
* @param string $id 要?jiǎng)h除的id
* @return bool 刪除成功則返回true,失敗則返回false
*/
public function delete($id="")
{
//判斷id是否存在
if (empty($id)){
$where = $this->where;
}else{
$where = "where id={$id}";
}
$sql = "delete from {$this->tableName} {$where}";
echo $sql;
//執(zhí)行并發(fā)送
return $this->exec($sql);
}
/**
* 修改操作
* @param array $data 要修改的數(shù)組
* @return bool 修改成功返回true,失敗返回false
*/
public function update($data){
//判斷是否是數(shù)組
if (!is_array($data)){
return $this;
}
//判斷是否是全是非法字段
if(empty($data)){
die("非法數(shù)據(jù)");
}
$str = "";
//過(guò)濾非法字段
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("請(qǐng)輸入條件");
}
//去掉最右側(cè)的逗號(hào)
$str = rtrim($str,",");
$sql = "update {$this->tableName} set {$str} {$this->where}";
return $this->exec($sql);
}
/**
* where條件
* @param string $where 輸入的where條件
* @return $this 返回自己,保證連貫操作
*/
public function where($where)
{
$this->where = "where ".$where;
return $this;
}
/**
* order排序條件
* @param string $order 以此為基準(zhǔn)進(jìn)行排序
* @return $this 返回自己,保證連貫操作
*/
public function order($order)
{
$this->order = "order by ".$order;
return $this;
}
/**
* limit條件
* @param string $limit 輸入的limit條件
* @return $this 返回自己,保證連貫操作
*/
public function limit($limit)
{
$this->limit = "limit ".$limit;
return $this;
}
/**
* 析構(gòu)方法
* 關(guān)閉數(shù)據(jù)庫(kù)連接
*/
public function __destruct()
{
mysqli_close($this->link);
}
}
//自行調(diào)試
$a = new Model("表名");
// var_dump($a->find(3));
// var_dump($a->select());
// var_dump($a->count());
// $res = $a->select();
//var_dump($res);
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php封裝db類(lèi)連接sqlite3數(shù)據(jù)庫(kù)的方法實(shí)例
- php db類(lèi)庫(kù)進(jìn)行數(shù)據(jù)庫(kù)操作
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫(kù)連接工具類(lèi)【定義與用法】
- PHP數(shù)據(jù)庫(kù)表操作的封裝類(lèi)及用法實(shí)例詳解
- PHP封裝的PDO數(shù)據(jù)庫(kù)操作類(lèi)實(shí)例
- PHP數(shù)據(jù)庫(kù)處理封裝類(lèi)實(shí)例
- php簡(jiǎn)單數(shù)據(jù)庫(kù)操作類(lèi)的封裝
- PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫(kù)操作類(lèi)與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫(kù)操作類(lèi)示例
- PHP封裝的數(shù)據(jù)庫(kù)模型Model類(lèi)完整示例【基于PDO】
- PHP封裝類(lèi)似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類(lèi)與簡(jiǎn)單應(yīng)用示例
相關(guān)文章
優(yōu)化WordPress中文章與評(píng)論的時(shí)間顯示
這篇文章主要介紹了優(yōu)化WordPress中文章與評(píng)論的時(shí)間顯示的方法,文中講解了設(shè)置相對(duì)時(shí)間內(nèi)顯示xxx時(shí)間以前及相對(duì)時(shí)間外顯示具體時(shí)間的方法,需要的朋友可以參考下2016-01-01
PHP項(xiàng)目多語(yǔ)言配置平臺(tái)實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了PHP項(xiàng)目多語(yǔ)言配置平臺(tái)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
PHP 5.3新特性命名空間規(guī)則解析及高級(jí)功能
本文介紹了PHP命名空間的一些術(shù)語(yǔ),其解析規(guī)則,以及一些高級(jí)功能的應(yīng)用,希望能夠幫助讀者在項(xiàng)目中真正使用命名空間。2010-03-03
php實(shí)現(xiàn)的讀取CSV文件函數(shù)示例
這篇文章主要介紹了php實(shí)現(xiàn)的讀取CSV文件函數(shù),結(jié)合實(shí)例形式分析了php讀取CSV文件及字符串、數(shù)組遍歷與轉(zhuǎn)換等操作技巧,需要的朋友可以參考下2017-02-02
php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn)
這篇文章主要介紹了php中有關(guān)合并某一字段鍵值相同的數(shù)組合并的改進(jìn),需要的朋友可以參考下2015-03-03

