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. mysql内存使用以及优化中需要的几点注意

    1.从内存中读取数据是微秒级别的.而从磁盘读则是毫秒级别的.二者相差一个数量级.所以想优化数据库,第一个要做到的就是优化io. 2.key_buffer_size[global]设置的内存区域大小缓存 ...

  2. React组件生命周期过程说明

    来自kiinlam github94 实例化 首次实例化 getDefaultProps getInitialState componentWillMount render componentDidM ...

  3. R语言—图像初阶

    dev.new() 创建一个新图像之前打开一个新的窗口 win.graph() 同上 pch() 指定绘制点时使用的符号 cex() 指定符号的大小,是一个数值,表示绘图符号相当于默认大小的缩放倍数 ...

  4. not子查询中有null值的时候 not in 会失效

    not in子查询中有null值的时候 not in 会失效 但是 in 的子查询中有null的 不会失效

  5. 无法进入adb shell,提示unknown host service的解决办法

    今天monkey的简易环境配置好后,准备开始monkey的压测工作,可是在命令控制窗口中输入无法进入adb shell,提示了错误 "unknown host service"

  6. Redis复制与可扩展集群搭建

    抄自:http://www.infoq.com/cn/articles/tq-redis-copy-build-scalable-cluster 讨论了Redis的常用数据类型与存储机制,本文会讨论一 ...

  7. 字典树的C++实现

    此次代码使用了诸多新鲜玩意,比如自动类型推断,模板类等等,感觉真的超级好玩啊. 自己做了一个简易的测试,程序的健壮性什么的还是比较差的,此程序留待以后进行功能补全. #pragma once #inc ...

  8. Esfog_UnityShader教程_UnityShader语法实例浅析

    距离上次首篇前言已经有一段时间了,一直比较忙,今天是周末不可以再拖了,经过我一段时间的考虑,我决定这一系列的教程会避免过于深入细节,一来可以避免一些同学被误导,二来会避免文章过于冗长难读, 三来可以让 ...

  9. 使用Struts2搭建登录注册示例

    使用Struts2来搭建mvc网站框架还是比较容易的,Struts2提供了各项辅助功能,保证了web开发的快速方便.下面使用struts2来搭建一个登录注册示例. 0 项目结构截图 1 搭建Strut ...

  10. Java值传递以及引用的传递、数组的传递!!

    转(http://blog.csdn.net/niuniu20008/article/details/2953785) 许多编程语言都有2种方法将参数传递给方法------按值传递和按引用传递. 与其 ...