温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

面向对象 MOdel

发布时间:2020-07-16 21:33:33 来源:网络 阅读:492 作者:方花 栏目:web开发

-----------------------------------config.php---------------------------------------------

<?php


//主机名

define('HOST','localhost');


//用户名

define('USER', 'root');


// 密码

define('PWD', '123456');


//数据库

define('DB','ss21');


//字符集

define('CHARSET','utf8');

--------------------------------index.php--------------------------------------------------

<?php

include './config.php';

//echo HOST;exit;

include './Model.class.php';

//实例化对象

$user = new Model('info');



$userlist =  $user->select();

var_dump($userlist);


?>

<table>

<tr>

<td>编号</td>

<td>编号</td>

<td>编号</td>

<td>编号</td>

<td>编号</td>

</tr>

<?php foreach($userlist as $val){ ?>

<tr>

<td>编号</td>

<td>编号</td>

<td>编号</td>

<td>编号</td>

<td>编号</td>

</tr>

<?php } ?>

</table>



-------------------------------Model.class.php---------------------------------------------


<?php


//定义一个操作数据库类

class Model{

public $tabName;//存储表名的

public $link;//连接数据的对象

public $limit;//  用于存储要显示条数

public $fields='*';// 用于存储要查询的字段

public $allFields; //缓存数据库字段

public $order;//用来存储排序的内容

public $where;//用来存储where条件

//构造方法里面连接数据库

public function __construct($tabName){

//初始化数据库连接

$this->getConnect();

//将你要操作的数据表存储起来

$this->tabName = $tabName;


//获取数据库字段

$this->getFields();

}

//查询

public function select(){

//5.

$sql="SELECT {$this->fields} FROM {$this->tabName} {$this->where} {$this->order} {$this->limit}";

echo $sql;

return $this->query($sql);

}


//查询一条结果

public function find($id){


//你查询的一条结果sql语句

$sql="SELECT {$this->fields} FROM {$this->tabName} WHERE id={$id}";

//调用发送sql语句函数

return $this->query($sql);

//var_dump($list);

}


//统计总条数

public function count(){

//返回值是字符串

$sql="SELECT COUNT(*)  total FROM {$this->tabName}";

$list = $this->query($sql);

return $list[0]['total'];

}



//增加

//$data['name']='zhansan';

//$data['age']=18;

//$data['sex']=1;

//$data['city']='沈阳';

public function add($data){

var_dump($data);


$key = array_keys($data);

$value = array_values($data);

$keys=join(',',$key);

$values = join("','",$value);


//var_dump($value);

//var_dump($key);exit;

$sql="INSERT INTO {$this->tabName}({$keys}) VALUES('{$values}')";

//echo $sql;exit;

return $this->execute($sql);


}

//删除思路 传递一个参数 这个参数是id值 

public function del($id=''){

//如果你没有传递过来id那么就必须传递where条件

if(empty($id)){

$where = $this->where;

}else{

$where = " WHERE id='{$id}'";

}


if(empty($where)){

return '请你传递一个条件进来';

}

$sql="DELETE FROM {$this->tabName} {$where}";

return $this->execute($sql);


}

//修改

//修改需要传递一个参数 这个参数要是要修改的字段的数组

public function update($arr){

//1.判断$arr 是不是数组

//2.判断你是否使用id作为修改内容还是使用where条件作为修改内容

//3.要将你的键和你的值拼接在一起 形成 name='zhangsan',sex='1',age='19',city='北京',

//4.将多出来的逗号取掉

//5.将得到的字符串防止到sql语句的set后面


$sql="UPDEATE {$this->tabName} SET  WHERE id=";

return $this->execute($sql);

}


//每页显示多少条

public function limit($limit){

$this->limit = ' limit '.$limit;

//echo $this->limit;

return $this;

}


//排除的内容应该怎么写

//order by

public function order($str){

$this->order =" ORDER BY {$str} ";

return $this;

}



//字段过滤  字段筛选  查询数据库内容 删除没有的字段

public function field($fields=array()){

// var_dump($fields);

if(!is_array($fields)){

//保证连贯操作

return $this;

}


//检测数据内容删除没有的字段

$fields = $this->check($fields);


if(empty($fields)){

return $this;

}

$this->fields = join(',',$fields);

//echo $this->fields;

return $this;

}

//where 

//$data['id']=1; id=1

//$data['id']=array('lt','10');

//$data['name']=array('like','张');


public function where($data){

//var_dump($data);exit;

//"where sex =1"

//where字符串拼接

//判断传递过来的必须是一个数组 而且不能为空

if(is_array($data) && !empty($data)){

//接受结果的

$result = array();

//循环传递进来的数组得到里面的键和值

foreach($data as $key=>$value){

//判断值是否为数组

if(is_array($value)){

//var_dump($value);exit;

$type = $value[0];

//判断你是什么样的条件将你所要的条件进行拼接

switch($type){

case 'like':

$result[]="{$key} LIKE '%{$value[1]}%'";

break;

case 'lt':

$result[]="{$key}<'{$value[1]}'";

break;

case 'gt':

$result[]="{$key}>'{$value[1]}'";

break;

case 'in':

$result[]="{$key} in({$value[1]})";

break;

}

}else{

//等于

$result[]="{$key}='{$value}'";

}

}

//echo ' WHERE '.join(' and ',$result);

//var_dump($result);exit;

}

//exit;

//where sex =1 and age >18;

//先把数组打开出来

$where = ' WHERE '.join(' and ',$result);

//echo $where;exit;

//var_dump($data);

//用来存储查询条件

$this->where = $where;

//保证连贯操作

return $this;


}





/****************辅助方法***************************/

//连接数据库的方法

protected function getConnect(){

$this->link=mysqli_connect(HOST,USER,PWD);

if(mysqli_connect_errno($this->link)){

echo mysqli_connect_error($this->link);exit;

}

mysqli_select_db($this->link,DB);

mysqli_set_charset($this->link,CHARSET);

}

//用于 添加 修改 删除

public function execute($sql){

$result = mysqli_query($this->link,$sql);

if($result && mysqli_affected_rows($this->link)>0){

if(mysqli_insert_id($this->link)){

return mysqli_insert_id($this->link);

}

return true; 

}else{

return false;

}

}


//查询操作

public function query($sql){

$result = mysqli_query($this->link,$sql);

if($result && mysqli_num_rows($result)>0){

while($row = mysqli_fetch_assoc($result)){

$list[]=$row;

}

}

return $list;

}

//获取数据库字段方法

protected function getFields(){

//查看表信息的数据库语句

$sql="DESC {$this->tabName}";

//发送SQL语句

$result = $this->query($sql);

//新建一个数组 用来存储数据库字段

$fields = array();

foreach($result as $value){

//var_dump($value);

$fields[]=$value['Field'];

}

//var_dump($fields);

//设置为缓存字段

$this->allFields = $fields;

}


//检测字段的方法

public function check($arr){

//遍历传递过来的数组  我们才能拿到数组中的键和值

foreach($arr as $key=>$value){

//echo $value.'<br/>';

//判断 你的值是否在缓存字段的数组中allFields

if(!in_array($value,$this->allFields)){

unset($arr[$key]);

}

}

//var_dump($arr);

return $arr;

}


//析构方法

public function __destruct(){

mysqli_close($this->link);

}

}



 







向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI