就是做一下整理 php pdo类操作。简化操作流程 更多内容 http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/pdo.class.php 点击链接加入群【微信开发探讨群】:http://jq.qq.com/?_wv=1027gt;http://www.oschina.net/code/snippet_2276613_46605
就是做一下整理 php pdo类操作。简化操作流程
更多内容
http://git.oschina.net/youkuiyuan/yky_test/blob/master/class/pdo.class.php
点击链接加入群【微信开发探讨群】:http://jq.qq.com/?_wv=1027&k=csncd9
群号:330393916
欢迎浏览:www.zcstrong.com
qq:2444756311
微信红包接口api - 拓展微信公众平台通用接口api(php版) --> http://www.oschina.net/code/snippet_2276613_46605 '','errmsg' => ''); //构造函数 - 初始化连接 public function __construct($dsn, $user, $password) { $this->dsn = $dsn; $this->user = $user; $this->password = $password; $this->pdoconnect(); } private function pdoconnect(){ try { $this->dbh = new pdo($this->dsn, $this->user, $this->password); return $this->dbh; } catch (pdoexception $e) { echo 'connection failed: ' . $e->getmessage(); exit(); } } //设置pdo参数 public function zcattribute($attribute, $value){ $this->dbh->setattribute($attribute, $value); } public function zclastid($name = null){ return $this->dbh->lastinsertid($name); } //数据库单语句执行操作 public function zcexec($param) { try { $rows = $this->dbh->exec($param);//影响行数 return $this->zclog(true, $rows); } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //格式化数据 public function zcquote($string){ return $this->dbh->quote($string); } //批量处理格式化数据 public function zcbatchquote($data){ $result = ; if(!empty($data) && (is_array($data) || is_object($data))){ foreach($data as $key => $value){ if(!empty($value) && (is_array($value) || is_object($value))){ $result[$key] = $this->zcbatchquote($value); } else{ $result[$key] = $this->zcquote($value); } } } else { $result = $this->zcquote($data); } return $result; } //数据库预处理操作 - 获取全部数据 public function zcfetchall($statement ,$parameter = null ,$type = pdo::fetch_assoc){ try{ $sth = $this->dbh->prepare($statement); //$sth->execute($parameter); $sth->execute($this->zcbatchquote($parameter)); $result = $sth->fetchall($type); if(!empty($result) && is_array($result)){ return $this->zclog(true, $result); } else{ return $this->zclog(true, null); } } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //数据库预处理操作 - 获取一行数据 public function zcfetchrow($statement ,$parameter = null ,$type = pdo::fetch_assoc){ try{ $sth = $this->dbh->prepare($statement); $sth->execute($this->zcbatchquote($parameter)); $result = $sth->fetch($type); if(!empty($result) && is_array($result)){ return $this->zclog(true, $result); } else{ return $this->zclog(true, null); } } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //数据库预处理操作 - 获取一个数据 public function zcfetchone($statement ,$parameter = null){ try{ $sth = $this->dbh->prepare($statement); $sth->execute($this->zcbatchquote($parameter)); $result = $sth->fetch(pdo::fetch_num); if(!empty($result) && is_array($result)){ return $this->zclog(true, $result[0]); } else{ return $this->zclog(true, null); } } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //开始事务 public function zcbegin(){ $this->dbh->begintransaction(); } //提交事务 public function zccommit(){ $this->dbh->commit(); } //回滚事务 public function zcrollback(){ $this->dbh->rollback(); } //预处理事务执行语句 public function zcptmtstquery($statement ,$parameter = null){ try{ $this->zcbegin(); $result = $this->dbh->prepare($statement)->execute($parameter); $this->zccommit(); return $this->zclog(true, $result); } catch (pdoexception $e) { $this->zcrollback(); return $this->zclog(false, $e->getmessage()); } } //预处理执行语句 public function zcptmquery($statement ,$parameter = null){ try{ $result = $this->dbh->prepare($statement)->execute($parameter); return $this->zclog(true, $result); } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //query执行 public function zcquery($statement,$type = pdo::fetch_assoc){ try{ $result = $this->dbh->query($statement,$type); return $this->zclog(true, $result); } catch (pdoexception $e) { return $this->zclog(false, $e->getmessage()); } } //日志log public function zclog($errcode , $errmsg){ $this->returnay = array(); $this->returnay['errcode'] = $errcode; $this->returnay['errmsg'] = $errmsg; $this->returnay['errtime'] = date(y-m-d h:i:s,time()); return $this->returnay; }}