php 单例 数据库 增删改查,php:单例模式+简单增删改查封装

1 <?php2 classsix{3 //定义私有静态变量存储 实例化对象

4 private static $ob;5 //定义表名

6 private $table;7 //定义条件

8 private $where;9 //定义公共pdo变量用于存储 链接数据库配置实例

10 public $pdo;11 //定义私有最终构造方法 防止 继承 子类重写 ;防止直接实例化对象

12 private function__construct()13 {14 $this->pdo = new PDO("mysql:host=127.0.0.1;dbname=blog","root","root");15

16 }17 //定义私有克隆 防止类外克隆

18 private function__clone()19 {20 echo "no1";21 }22 //私有静态方法 用于实例化类对象

23 public static functiongits(){24 //判断如果静态属性中是否有类对象 如果没有则 实例化该类 赋予 静态属性25 //如此便开辟了一块静态空间用来存储实例化对象 类外只需调用一次即可 节省了资源26 // 实现了单例模式的作用

27 if (!(self::$obinstanceof self)){28

29 self::$ob = newself();30

31 }32

33 return self::$ob;34

35 }36

37 //单删 批删封装

38 public function deleteAll($id){39 //判断$id 是否有值

40 if (empty($id)){41

42 return false;43 }44 //取数组中 values值

45 $key = array_values($id);46 //取得健为零值为要删除数据值的一维数组

47 @$num = count($key[0]);48 //循环 数组$id

49 foreach ($id as $k => $v){50 //$str = 数据库主键id字段

51 $str = $k;52 //idss = 要删除的数据id

53 $idss = $v;54

55 }56 $ids = ' where ';57 //判断 如果要删除的数据为数组并大于一条 则拼接为 id in (11,12,13)形式

58 if (is_array($id) && $num > 1){59

60 $ids .= $str ." in " . '('.implode(",",$idss) .')';61 //数组等于1条(这个count)值是判断 要删除的数据个数为162 //拼接成 id=1 的形式

63 }else if (is_array($id) && count($id) == 1){64

65 $ids .= $str . ' = ' . $idss;66

67 }68 //返回 实现单删多删

69 return $this->pdo->exec("delete from $this->table $ids");70 }71

72 //修改

73

74 public function update($data){75

76 $str = '';77 //循环数组拼接成 id=1,name=小明的形式

78 foreach ($data as $k => $v){79

80 $str .= "," . $k .'='. "'$v'";81 }82 $str = substr($str,1);83

84 //返回数组实现修改

85 return $this->pdo->exec("update $this->table set $str $this->where");86

87 }88

89 //定义传入的表名

90 public function tables($table){91

92 $this->table = $table;93 return $this;94

95 }96

97 public function where($where){98

99 if (empty($where)){100

101 return false;102 }103 $str = ' where ';104 if (is_array($where)){105

106 foreach ($where as $k => $v){107

108 $str .= $k .' = '. "'$v'" . ' and ';109 }110 $str = rtrim($str,' and ');111

112 }else{113

114 $str .= $where;115

116 }117 return $this->where = $str;118

119 }120

121 //单挑查询

122 public functionget(){123

124 return $this->pdo->query("select * from $this->table $this->where")->fetch(PDO::FETCH_ASSOC);125

126 }127

128 //添加处理数据

129 public function insert($data){130 //找到key 循环遍历成 id,name,sex 的形式

131 $keyArr = array_keys($data);132 $str = '';133 foreach ($keyArr as $k => $v){134

135 $str .= "," . $v;136 }137 $str = substr($str,1);138 //找到 value 遍历成 '1','小明','男' 的形式

139 $valArr = array_values($data);140 $strs = '';141 foreach ($valArr as $kk => $vv){142 $strs .= "," . "'$vv'";143 }144 $strs = substr($strs,1);145

146 //执行则添加成功

147 return $this->pdo->exec("insert into $this->table ($str) values ($strs)");148

149 }150

151 }152

153 $ob = six::gits();154

155 //单查询156 //$ob->tables('student')->where(['s_id'=>'08','s_name'=>'王菊']);157 //$data = $ob->get();158 //添加159 //$data = $ob->tables('student')->insert(['s_id'=>'14','s_name'=>'赵雷','s_birth'=>'1990-01-01','s_sex'=>'男']);160 //删除(单删 批删)161 //$data = $ob->tables('student')->deleteAll(['s_id'=>14]);162 //修改

163 $ob->tables('student')->where(['s_id'=>'08']);164 $data = $ob->update(['s_name'=>'李云','s_sex'=>'男']);165

166 var_dump($data);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值