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底层存 ...
随机推荐
- 验证视图状态MAC失败。如果此应用程序由网络场或群集承载,请确保配置指定了相同的validationKey和验证算法(转)
原文转自:http://www.cnblogs.com/Setme/archive/2012/06/05/2537084.html 验证视图状态 MAC 失败.如果此应用程序由网络场或群集承载,请确保 ...
- Selenium 元素定位
selenium通过driver.findElement(By selector)来定位元素,selector在selenium-java.jar中,里面的方法一共就8种,如下图: 基本定义: By. ...
- centos安装mongodb 3.2.9
centos 6.5 x64 1.下载地址:用迅雷下载,直接下载下不动 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2 ...
- 一个简易的反射类库NMSReflector
转自:http://blog.csdn.net/lanx_fly/article/details/53914338 背景简介 以前看过一些代码,是简单的读取SqlReader然后赋值给Model,我不 ...
- Xcode插件安装 错选了Skip Bundle解决办法
1.首先找到Xcode的UUID,在终端运行defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID ...
- JMeter压测Rest请求
下载及安装 官网下载JMeter3.0: 找到bin目录下的jmeter.bat启动: 压测Rest请求 1.添加线程组 路径:右键“测试计划”->添加“Threads(Users)”-> ...
- 【linux】之相关命令
防火墙 ) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off ) 即时生效,重启后失效 开启: service iptables s ...
- HTML input-file 上传类型控制
HTML input-file 上传类型控制 input file 属性 accept 表示可以选择的文件MIME类型,多个MIME类型用英文逗号分开,常用的MIME类型见下表. 只能选择png和gi ...
- 一个github账户多台电脑代码提交
在实际工作生活中,我们可能不一定仅仅在一台电脑上编码,比如:我们平时在单位电脑1上写代码,提交代码到github账户,而我们也可能会在在家里的电脑2上继续工作,提交代码,这样就是在不同的电脑上提交代码 ...
- Hbase数据导入导出
平时用于从生产环境hbase到导出数据到测试环境. 导入数据: import java.io.BufferedReader; import java.io.File; import java.io.F ...