pdo 整套类的封装,保存修改查询
<?php/****/class Db{private $host = '';private $port = '';private $user = '';private $pass = '';private $dbname = '';private $charset='';private $tbp = '';private $db = '';/*** 返回 pdo对象*/public function getDb(){return $this->db;}/*** @param $tbp 表名前辍*/public function setTbp($tbp=''){$this->tbp = $tbp;}public function __construct($host='localhost',$port='3306',$user='root',$pass='',$dbname='fzdb',$charset='utf8',$tbp='fz_'){$dsn = "mysql:host=$host;port=$port;dbname=$dbname";$opt = [1002 =>"set names $charset"];try {$this->db = new PDO ( $dsn, 'root', '', $opt );$this->host = $host;$this->port = $port;$this->user = $user;$this->pass = $pass;$this->dbname = $dbname;$this->tbp = $tbp;$this->charset = $charset;} catch ( Exception $e ) {exit ( '数据库服务器连接失败,程序终止' );}}/**** @param unknown $t 表名,不含执行时会自动加上表前辍* @param unknown $d 要插入的数据是关联数组*/public function gomsg($url='',$msg=''){echo '<script>';echo "alert('$msg');";echo "location.href='$url'";echo '</script>';}public function save($t,$d){$t = $this->tbp.$t;foreach($d as $key=>$v){$kk[] = $key;$vv[] = ':'.$key;}$kk = implode(',',$kk);$vv = implode(',',$vv);$stmt = $this->db->prepare("insert into $t($kk) values($vv)");$stmt->execute($d);$stmt->closeCursor();}public function query($t,$f='*',$w='1=1',$o='',$l='limit 10'){$t = $this->tbp.$t;$stmt = $this->db->prepare("select $f from $t where $w $o $l");$stmt->execute();$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);$stmt->closeCursor();return $rs;}public function delete($t,$w='1=1'){$t = $this->tbp.$t;$stmt = $this->db->prepare("delete from $t where $w");$stmt->execute();$stmt->closeCursor();}public function getcount($t,$w='1=1'){$t = $this->tbp.$t;$stmt = $this->db->prepare("select count(*) from $t where $w");$stmt->execute();$stmt->bindColumn(1, $c);$stmt->fetchAll(PDO::FETCH_NUM);return $c;}public function updatenum($t,$f,$w='1=1'){$t = $this->tbp.$t;$stmt = $this->db->prepare("update $t set $f where $w");$stmt->execute();$stmt->closeCursor();}public function pager($t,$currpage=1,$f='*',$pagesize=3,$w='1=1',$o='',$ty=''){$recordcount = $this->getcount($t,$w);$t = $this->tbp.$t;$stmt = $this->db->prepare("select $f from $t where $w $o limit ?,?");$pagecount = ceil($recordcount/$pagesize);$currpage = $currpage<1 ? 1 : $currpage;$currpage = $currpage>$pagecount ? $pagecount : $currpage;$start = $currpage*$pagesize - $pagesize;$stmt->bindParam(1, $start, PDO::PARAM_INT);$stmt->bindParam(2, $pagesize, PDO::PARAM_INT);$stmt->execute();$row[] = $stmt->fetchAll(PDO::FETCH_ASSOC);$first = 1;$end = 10;$pages = '<div class="page">';if($currpage>=7){$first = $currpage-5;$end = $first+$end-1;}if($currpage>1){$prev = $currpage-1;if($first>1){$pages.="<a href=?".$ty."p=1>首页</a><a href=?".$ty."p=$prev>上一页</a>";}else{$pages.="<a href=?".$ty."p=$prev>上一页</a>";}}for($i=$first;$i<=$end;$i++){if($i>$pagecount){break;}if($i==$currpage){$pages.='<a class="checked">'.$i.'</a>';continue;}$pages.="<a href=?".$ty."p=$i>$i</a>";}if($currpage<$pagecount){$next = $currpage+1;$pages.="<a href=?".$ty."p=$next>下一页</a>";}if($end<$pagecount){$pages.="<a href=?".$ty."p=$pagecount>尾页</a>";}$row[] = $pages.'</div>';$row[] = $pagesize;$row[] = $pagecount;$row[] = $recordcount;$row[] = $currpage;return $row;}public function css1(){$css = <<<css<style>.page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}.page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}.page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}.page a:visited,.page a:link{color:#333;}.page a:active{color:#3B3B3B;}</style>css;echo $css;}public function md5($p,$c='webrx'){$s1 = md5($p.$c);$s2 = sha1($p.$c);$sok = substr($s1,0,6).substr($s2,0,6);$sok .= substr($s1,12,5).substr($s2,22,5);$sok .= substr($s1,22,5).substr($s2,32,5);return $sok;}public function update($t,$d,$w='1=1'){$t = $this->tbp.$t;foreach($d as $key=>$v){$kk[] = $key.'=:'.$key;}$kk = implode(',',$kk);$stmt = $this->db->prepare("update $t set $kk where $w");$stmt->execute($d);$stmt->closeCursor();}public function __destruct(){unset($this->db);}}
pdo 整套类的封装,保存修改查询的更多相关文章
- 一个强大的封装好的pdo处理类
php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了.不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类. ...
- Java—类的封装、继承与多态
一.类和对象 1.类 类是数据以及对数据的一组操作的封装体. 类声明的格式: 类声明 { 成员变量的声明: 成员方法的声明及实现: } 1.1 声明类 [修饰符] class 类<泛型> ...
- Java---对象与类的封装
一.类和对象: package cn.hncu.Myclasslearn; /** * * @author hncu_chx * * Mylove amin */ /**类是一种数据类型,声明一个类就 ...
- PHP 类的封装和使用
类:相似的数据和数据操作的封装 class 成员量:普通的量加上一定的修饰就变成了成员量 public,protected,private 成员方法:普通的函数,加上一定的修饰,放入到类中就变成了成 ...
- 类的封装(property)
封装 封装程序的主要原因:保护隐私:而封装方法的主要原因是:隔离复杂的执行过程 property的特性 将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执 ...
- 关于PHP建立数据库访问类的封装以及操作php单例模式连接数据库封装类
建立数据库访问类的封装 <?php class DBDA { public $host = "localhost"; //服务器地址 public $ui ...
- 模块的封装之C语言类的封装
[微知识]模块的封装(一):C语言类的封装 是的,你没有看错,我们要讨论的是C语言而不是C++语言中类的封装.在展开知识点之前,我首先要 重申两点: 1.面向对象是一种思想,基本与所用的语言是无关的. ...
- 第三篇 :微信公众平台开发实战Java版之请求消息,响应消息以及事件消息类的封装
微信服务器和第三方服务器之间究竟是通过什么方式进行对话的? 下面,我们先看下图: 其实我们可以简单的理解: (1)首先,用户向微信服务器发送消息: (2)微信服务器接收到用户的消息处理之后,通过开发者 ...
- 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装
上一篇文章提过,HSSF的用户模式会导致读取海量数据时很慢,所以我们采用的是事件驱动模式.这个模式类似于xml的sax解析.需要实现一个接口,HSSFListener接口. 原理:根据excel底层存 ...
随机推荐
- mysql内存使用以及优化中需要的几点注意
1.从内存中读取数据是微秒级别的.而从磁盘读则是毫秒级别的.二者相差一个数量级.所以想优化数据库,第一个要做到的就是优化io. 2.key_buffer_size[global]设置的内存区域大小缓存 ...
- React组件生命周期过程说明
来自kiinlam github94 实例化 首次实例化 getDefaultProps getInitialState componentWillMount render componentDidM ...
- R语言—图像初阶
dev.new() 创建一个新图像之前打开一个新的窗口 win.graph() 同上 pch() 指定绘制点时使用的符号 cex() 指定符号的大小,是一个数值,表示绘图符号相当于默认大小的缩放倍数 ...
- not子查询中有null值的时候 not in 会失效
not in子查询中有null值的时候 not in 会失效 但是 in 的子查询中有null的 不会失效
- 无法进入adb shell,提示unknown host service的解决办法
今天monkey的简易环境配置好后,准备开始monkey的压测工作,可是在命令控制窗口中输入无法进入adb shell,提示了错误 "unknown host service"
- Redis复制与可扩展集群搭建
抄自:http://www.infoq.com/cn/articles/tq-redis-copy-build-scalable-cluster 讨论了Redis的常用数据类型与存储机制,本文会讨论一 ...
- 字典树的C++实现
此次代码使用了诸多新鲜玩意,比如自动类型推断,模板类等等,感觉真的超级好玩啊. 自己做了一个简易的测试,程序的健壮性什么的还是比较差的,此程序留待以后进行功能补全. #pragma once #inc ...
- Esfog_UnityShader教程_UnityShader语法实例浅析
距离上次首篇前言已经有一段时间了,一直比较忙,今天是周末不可以再拖了,经过我一段时间的考虑,我决定这一系列的教程会避免过于深入细节,一来可以避免一些同学被误导,二来会避免文章过于冗长难读, 三来可以让 ...
- 使用Struts2搭建登录注册示例
使用Struts2来搭建mvc网站框架还是比较容易的,Struts2提供了各项辅助功能,保证了web开发的快速方便.下面使用struts2来搭建一个登录注册示例. 0 项目结构截图 1 搭建Strut ...
- Java值传递以及引用的传递、数组的传递!!
转(http://blog.csdn.net/niuniu20008/article/details/2953785) 许多编程语言都有2种方法将参数传递给方法------按值传递和按引用传递. 与其 ...