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底层存 ...
随机推荐
- [转]编译安装libevent,memcache,以及php的memcached扩展
一 安装libevent 1.去官网http://libevent.org/ 下载最新源码,我用的是libevent-2.0.20-stable.tar.gz 2.解压到/usr/src目录 ,执行命 ...
- Javascript模式(第五章对象创建模式)------读书笔记
一 命名空间模式 1 命名空间模式的代码格式 var MYAPP={ name:"", version:"1.0", init:function(){ } }; ...
- GraphicsMagick+Im4Java在windows和linux下的配置
GraphicsMagick介绍及安装 Im4Java包为: im4java-1.2.0.jar 直接在lib下引用即可 GraphicsMagick的安装如下: windows下: 安装:Graph ...
- linux下重置mysql的root密码
# /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & # my ...
- AngularJS---表达式
AngularJS的表达式是放在{{}}里面,用{{ }}符号将一个变量绑定到$scope上. angularJS中的表达式有如下特点: 1.只能在其所属作用域内部 所有的表达式都在其所属的作用域内部 ...
- <<Numerical Analysis>>笔记
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...
- JUnit 简介
一.简介 JUnit是一个开源的java单元测试框架.在1997年,由 Erich Gamma 和 Kent Beck 开发完成.这两个牛人中 Erich Gamma 是 GOF 之一:Kent Be ...
- 51nod 1643 小Q的家庭作业
题意: f(n) = sigma(gcd(i,n)) 1 <= i <= n g(n) = sigma(f(d)) d | n n = x1 * x2 * ... * xm 其中 ...
- java.lang.NoClassDefFoundError 解决方案
http://stackoverflow.com/questions/9870995/android-java-lang-noclassdeffounderror 像网络了上说的一般这种问题是 运行时 ...
- Python单链表实现
class Node(): def __init__(self,InitDate): self.Date=InitDate self.next=None def setNext(self,newnex ...