1. <?php
  2. /**
  3. *
  4. */
  5. class Db{
  6. private $host = '';
  7. private $port = '';
  8. private $user = '';
  9. private $pass = '';
  10. private $dbname = '';
  11. private $charset='';
  12. private $tbp = '';
  13. private $db = '';
  14. /**
  15. * 返回 pdo对象
  16. */
  17. public function getDb(){
  18. return $this->db;
  19. }
  20. /**
  21. * @param $tbp 表名前辍
  22. */
  23. public function setTbp($tbp=''){
  24. $this->tbp = $tbp;
  25. }
  26. public function __construct($host='localhost',$port='3306',$user='root',$pass='',$dbname='fzdb',$charset='utf8',$tbp='fz_'){
  27. $dsn = "mysql:host=$host;port=$port;dbname=$dbname";
  28. $opt = [1002 =>"set names $charset"];
  29. try {
  30. $this->db = new PDO ( $dsn, 'root', '', $opt );
  31. $this->host = $host;
  32. $this->port = $port;
  33. $this->user = $user;
  34. $this->pass = $pass;
  35. $this->dbname = $dbname;
  36. $this->tbp = $tbp;
  37. $this->charset = $charset;
  38. } catch ( Exception $e ) {
  39. exit ( '数据库服务器连接失败,程序终止' );
  40. }
  41. }
  42. /**
  43. *
  44. * @param unknown $t 表名,不含执行时会自动加上表前辍
  45. * @param unknown $d 要插入的数据是关联数组
  46. */
  47. public function gomsg($url='',$msg=''){
  48. echo '<script>';
  49. echo "alert('$msg');";
  50. echo "location.href='$url'";
  51. echo '</script>';
  52. }
  53. public function save($t,$d){
  54. $t = $this->tbp.$t;
  55. foreach($d as $key=>$v){
  56. $kk[] = $key;
  57. $vv[] = ':'.$key;
  58. }
  59. $kk = implode(',',$kk);
  60. $vv = implode(',',$vv);
  61. $stmt = $this->db->prepare("insert into $t($kk) values($vv)");
  62. $stmt->execute($d);
  63. $stmt->closeCursor();
  64. }
  65. public function query($t,$f='*',$w='1=1',$o='',$l='limit 10'){
  66. $t = $this->tbp.$t;
  67. $stmt = $this->db->prepare("select $f from $t where $w $o $l");
  68. $stmt->execute();
  69. $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  70. $stmt->closeCursor();
  71. return $rs;
  72. }
  73. public function delete($t,$w='1=1'){
  74. $t = $this->tbp.$t;
  75. $stmt = $this->db->prepare("delete from $t where $w");
  76. $stmt->execute();
  77. $stmt->closeCursor();
  78. }
  79. public function getcount($t,$w='1=1'){
  80. $t = $this->tbp.$t;
  81. $stmt = $this->db->prepare("select count(*) from $t where $w");
  82. $stmt->execute();
  83. $stmt->bindColumn(1, $c);
  84. $stmt->fetchAll(PDO::FETCH_NUM);
  85. return $c;
  86. }
  87. public function updatenum($t,$f,$w='1=1'){
  88. $t = $this->tbp.$t;
  89. $stmt = $this->db->prepare("update $t set $f where $w");
  90. $stmt->execute();
  91. $stmt->closeCursor();
  92. }
  93. public function pager($t,$currpage=1,$f='*',$pagesize=3,$w='1=1',$o='',$ty=''){
  94. $recordcount = $this->getcount($t,$w);
  95. $t = $this->tbp.$t;
  96. $stmt = $this->db->prepare("select $f from $t where $w $o limit ?,?");
  97. $pagecount = ceil($recordcount/$pagesize);
  98. $currpage = $currpage<1 ? 1 : $currpage;
  99. $currpage = $currpage>$pagecount ? $pagecount : $currpage;
  100. $start = $currpage*$pagesize - $pagesize;
  101. $stmt->bindParam(1, $start, PDO::PARAM_INT);
  102. $stmt->bindParam(2, $pagesize, PDO::PARAM_INT);
  103. $stmt->execute();
  104. $row[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
  105. $first = 1;
  106. $end = 10;
  107. $pages = '<div class="page">';
  108. if($currpage>=7){
  109. $first = $currpage-5;
  110. $end = $first+$end-1;
  111. }
  112. if($currpage>1){
  113. $prev = $currpage-1;
  114. if($first>1){
  115. $pages.="<a href=?".$ty."p=1>首页</a><a href=?".$ty."p=$prev>上一页</a>";
  116. }else{
  117. $pages.="<a href=?".$ty."p=$prev>上一页</a>";
  118. }
  119. }
  120. for($i=$first;$i<=$end;$i++){
  121. if($i>$pagecount){
  122. break;
  123. }
  124. if($i==$currpage){
  125. $pages.='<a class="checked">'.$i.'</a>';
  126. continue;
  127. }
  128. $pages.="<a href=?".$ty."p=$i>$i</a>";
  129. }
  130. if($currpage<$pagecount){
  131. $next = $currpage+1;
  132. $pages.="<a href=?".$ty."p=$next>下一页</a>";
  133. }
  134. if($end<$pagecount){
  135. $pages.="<a href=?".$ty."p=$pagecount>尾页</a>";
  136. }
  137. $row[] = $pages.'</div>';
  138. $row[] = $pagesize;
  139. $row[] = $pagecount;
  140. $row[] = $recordcount;
  141. $row[] = $currpage;
  142. return $row;
  143. }
  144. public function css1(){
  145. $css = <<<css
  146. <style>
  147. .page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}
  148. .page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}
  149. .page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}
  150. .page a:visited,.page a:link{color:#333;}
  151. .page a:active{color:#3B3B3B;}
  152. </style>
  153. css;
  154. echo $css;
  155. }
  156. public function md5($p,$c='webrx'){
  157. $s1 = md5($p.$c);
  158. $s2 = sha1($p.$c);
  159. $sok = substr($s1,0,6).substr($s2,0,6);
  160. $sok .= substr($s1,12,5).substr($s2,22,5);
  161. $sok .= substr($s1,22,5).substr($s2,32,5);
  162. return $sok;
  163. }
  164. public function update($t,$d,$w='1=1'){
  165. $t = $this->tbp.$t;
  166. foreach($d as $key=>$v){
  167. $kk[] = $key.'=:'.$key;
  168. }
  169. $kk = implode(',',$kk);
  170. $stmt = $this->db->prepare("update $t set $kk where $w");
  171. $stmt->execute($d);
  172. $stmt->closeCursor();
  173. }
  174. public function __destruct(){
  175. unset($this->db);
  176. }
  177. }

pdo 整套类的封装,保存修改查询的更多相关文章

  1. 一个强大的封装好的pdo处理类

    php5.5后就不支持mysql扩展了,也就是说这以后都不能使用msyql_conncet之类的函数了.不过没有关系,pdo比mysql有更多优势,写法也很简单,下面贴出一个来自互联网的pdo处理类. ...

  2. Java—类的封装、继承与多态

    一.类和对象 1.类 类是数据以及对数据的一组操作的封装体. 类声明的格式: 类声明 { 成员变量的声明: 成员方法的声明及实现: } 1.1 声明类 [修饰符] class 类<泛型> ...

  3. Java---对象与类的封装

    一.类和对象: package cn.hncu.Myclasslearn; /** * * @author hncu_chx * * Mylove amin */ /**类是一种数据类型,声明一个类就 ...

  4. PHP 类的封装和使用

    类:相似的数据和数据操作的封装  class 成员量:普通的量加上一定的修饰就变成了成员量 public,protected,private 成员方法:普通的函数,加上一定的修饰,放入到类中就变成了成 ...

  5. 类的封装(property)

    封装 封装程序的主要原因:保护隐私:而封装方法的主要原因是:隔离复杂的执行过程 property的特性 将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执 ...

  6. 关于PHP建立数据库访问类的封装以及操作php单例模式连接数据库封装类

    建立数据库访问类的封装 <?php   class DBDA {     public $host = "localhost"; //服务器地址     public $ui ...

  7. 模块的封装之C语言类的封装

    [微知识]模块的封装(一):C语言类的封装 是的,你没有看错,我们要讨论的是C语言而不是C++语言中类的封装.在展开知识点之前,我首先要 重申两点: 1.面向对象是一种思想,基本与所用的语言是无关的. ...

  8. 第三篇 :微信公众平台开发实战Java版之请求消息,响应消息以及事件消息类的封装

    微信服务器和第三方服务器之间究竟是通过什么方式进行对话的? 下面,我们先看下图: 其实我们可以简单的理解: (1)首先,用户向微信服务器发送消息: (2)微信服务器接收到用户的消息处理之后,通过开发者 ...

  9. 025医疗项目-模块二:药品目录的导入导出-HSSF导入类的封装

    上一篇文章提过,HSSF的用户模式会导致读取海量数据时很慢,所以我们采用的是事件驱动模式.这个模式类似于xml的sax解析.需要实现一个接口,HSSFListener接口. 原理:根据excel底层存 ...

随机推荐

  1. [转]编译安装libevent,memcache,以及php的memcached扩展

    一 安装libevent 1.去官网http://libevent.org/ 下载最新源码,我用的是libevent-2.0.20-stable.tar.gz 2.解压到/usr/src目录 ,执行命 ...

  2. Javascript模式(第五章对象创建模式)------读书笔记

    一 命名空间模式 1 命名空间模式的代码格式 var MYAPP={ name:"", version:"1.0", init:function(){ } }; ...

  3. GraphicsMagick+Im4Java在windows和linux下的配置

    GraphicsMagick介绍及安装 Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:Graph ...

  4. linux下重置mysql的root密码

    # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & # my ...

  5. AngularJS---表达式

    AngularJS的表达式是放在{{}}里面,用{{ }}符号将一个变量绑定到$scope上. angularJS中的表达式有如下特点: 1.只能在其所属作用域内部 所有的表达式都在其所属的作用域内部 ...

  6. <<Numerical Analysis>>笔记

    2ed,  by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...

  7. JUnit 简介

    一.简介 JUnit是一个开源的java单元测试框架.在1997年,由 Erich Gamma 和 Kent Beck 开发完成.这两个牛人中 Erich Gamma 是 GOF 之一:Kent Be ...

  8. 51nod 1643 小Q的家庭作业

    题意: f(n) = sigma(gcd(i,n))  1 <= i <= n g(n) = sigma(f(d))    d | n n = x1 * x2 * ... * xm 其中 ...

  9. java.lang.NoClassDefFoundError 解决方案

    http://stackoverflow.com/questions/9870995/android-java-lang-noclassdeffounderror 像网络了上说的一般这种问题是 运行时 ...

  10. Python单链表实现

    class Node(): def __init__(self,InitDate): self.Date=InitDate self.next=None def setNext(self,newnex ...