您好,欢迎访问一九零五行业门户网

封装了一个PDO的类,希望CSDN的大牛们指点一下不足之处

封装了一个pdo的类,希望csdn的大牛们指点一下不足之处。
大师们,come on !
set names , ); public function __construct($dsn, $user = '', $pass = '', $persistent = false, $charset = utf8){ $this->options[pdo::mysql_attr_init_command] .= $charset; if($persistent){ $this->options[pdo::attr_persistent] = true; } $this->pdo = new pdo($dsn, $user, $pass, $this->options); } /** 全局属性设置,包括:列名格式和错误提示类型 可以使用数字也能直接使用参数 */ public function setattr($param, $val = ''){ if(is_array($param)){ foreach($param as $key => $val){ $this->pdo->setattribute($key, $val); } }else{ if($val != '' ){ $this->pdo->setattribute($param, $val); }else{ return false; } } } /** 生成一个编译好的sql语句模版 你可以使用 ? :name 的形式 返回一个statement对象 */ public function prepare($sql){ if(empty($sql)){ return false; } $this->statement = $this->pdo->prepare($sql); return $this->statement; } /** 执行sql语句,一般用于 增、删、更新或者设置 返回影响的行数 */ public function exec($sql){ if(empty($sql)){ return false; } try{ return $this->pdo->exec($sql); }catch(exception $e){ return $e->getmessage(); } } /** 执行有返回值的查询,返回pdostatement 可以通过链式操作,可以通过这个类封装的操作获取数据 */ public function query($sql){ if(empty($sql)){ return false; } $this->statement = $this->pdo->query($sql); return $this->statement; } /** 开启事务 */ public function begintransaction(){ return $this->pdo->begintransaction(); } /** 提交事务 */ public function commit(){ return $this->pdo->commit(); } /** 事务回滚 */ public function rollback(){ return $this->pdo->rollback(); } public function lastinertid(){ return $this->pdo->lastinsertid(); } //** pdostatement 类操作封装 **// /** 让模版执行sql语句,1、执行编译好的 2、在执行时编译 */ public function execute($param = ){ if(is_array($param)){ try{ return $this->statement->execute($param); }catch (exception $e){ //return $this->errorinfo(); return $e->getmessage(); } }else{ try{ return $this->statement->execute(); }catch(exception $e){ /* 返回的错误信息格式 [0] => 42s22 [1] => 1054 [2] => unknown column 'col' in 'field list' return $this->errorinfo(); */ return $e->getmessage(); } } } /** 参数1说明: pdo::fetch_both 也是默认的,两者都有(索引,关联) pdo::fetch_assoc 关联数组 pdo::fetch_num 索引 pdo::fetch_obj 对象 pdo::fetch_lazy 对象 会附带querystring查询sql语句 pdo::fetch_bound 如果设置了bindcolumn,则使用该参数 */ public function fetch($fetch_type = pdo::fetch_assoc){ if(is_object($this->statement)){ return $this->statement->fetch($fetch_type); } return false; } /** 参数1说明: pdo::fetch_both 也是默认的,两者都有(索引,关联) pdo::fetch_assoc 关联数组 pdo::fetch_num 索引 pdo::fetch_obj 对象 pdo::fetch_column 指定列 参数2可以指定要获取的列 pdo::fetch_class 指定自己定义的类 pdo::fetch_func 自定义类 处理返回的数据 pdo_fetch_bound 如果你需要设置bindcolumn,则使用该参数 参数2说明: 给定要处理这个结果的类或函数 */ public function fetchall($fetch_type = pdo::fetch_assoc, $handle = ''){ if(empty($handle)){ return $this->statement->fetchall($fetch_type); } return $this->statement->fetchall($fetch_type, $handle); } /** 以对象形式返回 结果 跟fetch(pdo::fetch_obj)一样 */ public function fetchobject($class_name){ if(empty($clss_name)){ return $this->statement->fetchobject(); } return $this->statement->fetchobject($class_name); } public function fetchcolumn($intcolumn = 0){ return $this->statement->fetchcolumn($intcolumn); } /** public function bindcolumn($array=array(),$type=extr_overwrite){ if(count($array)>0){ extract($array,$type); } //$this->statement->bindcolumn() } */ /** 以引用的方式绑定变量到占位符(可以只执行一次prepare,执行多次bindparam达到重复使用的效果) */ public function bindparam($parameter, $variable, $data_type = 'str', $length = 0){ switch ($data_type){ case 'str': $data_type = pdo::param_str; break; case 'int': $data_type = pdo::param_int; break; default : $data_type = ''; break; } return $this->statement->bindparam($parameter, $variable, $data_type, $length); } /** 返回statement记录集的行数 */ public function rowcount(){ return $this->statement->rowcount(); } public function count(){ return $this->statement->rowcount(); } public function columncount(){ $this->statement->execute(); return $this->statement->columncount(); } public function getcolumnmeta($intcolumn){ return $this->statement->getcolumnmeta($intcolumn); } /** 关闭 */ public function close(){ return $this->statement->closecursor(); } public function closecursor(){ return $this->statement->closecursor(); } /** 返回错误信息也包括错误号 */ private function errorinfo(){ return $this->statement->errorinfo(); } /** 返回错误号 */ private function errorcode(){ return $this->statement->errorcode(); } //简化操作 public function insert($table, $data){ if(!is_array($data)){ return false; } $cols = array(); $vals = array(); foreach($data as $key => $val){ $cols[] = $key; $vals[] = ' . $val . '; } $sql = insert into {$table} (; $sql .= implode(,, $cols) . ) values (; $sql .= implode(,, $vals) . ); return $this->exec($sql); } public function insertbind($table, $arraydata){ if(!is_array($arraydata)){ return false; } $vals = array_keys($arraydata); $cols = array(); /* $arrayobject = new arrayobject( $arraydata ); $iterator = $arrayobject->getiterator(); while($iterator->valid()) { $vals[] = ':' . $iterator->key() . ''; $iterator->next(); } */ $c = implode('', $vals); $cols = array_filter(explode(':', $c)); $sql = insert into {$table} (; $sql .= implode(,, $cols) . ) values (; $sql .= implode(,, $vals) . ); $this->statement = $this->pdo->prepare($sql); $this->statement->execute($arraydata); return $this->statement->rowcount(); } public function update($table, $data, $where){ if(!is_array($data)){ return false; } $set = array(); foreach($data as $key => $val){ $set[] = $key . =' . $val . '; } $sql = update {$table} set ; $sql .= implode(,, $set); $sql .= where . $where; return $this->exec($sql); } public function updatebind($sql, $arraywhere){ if(empty($sql) || !is_array($arraywhere)){ return false; } $this->statement = $this->pdo->prepare($sql); $this->statement->execute($arraywhere); return $this->statement->rowcount(); } public function delete($table, $where){ if(empty($table) || empty($where)){ return false; } $sql = delete from {$table} where . $where; return $this->exec($sql); } public function deletebind($sql, $arraywhere){ if(empty($sql) || !is_array($arraywhere)){ return false; } $this->statement = $this->pdo->prepare($sql); $this->statement->execute($arraywhere); return $this->statement->rowcount(); }}?>
毕竟在一个项目当中,不可能到处的写pdo自身的一些东西。也不要说pdo已经封装好了,无须再次封装之类的废话。
回复讨论(解决方案) 几点建议:
1、如果我
$db = new pdox;
$a = $db->query($sql1);
$b = $db->query($sql2);
print_r($a->fetchall());
print_r($b->fetchall());
可以吗?显然是不可以的,因为
$this->statement = $this->pdo->query($sql);
前一次的查询结果被后一次的覆盖了
2、事务(transaction)操作宜封装成整体,只传入一组 sql 而隐藏相关操作
3、没考虑使用存储过程
4、pdox 可直接继承于 pdo 而无需抄写 pdo 已有方法
总的来说还不错,但感觉上有些方法过于细琐,可以有必要的整合在一起
几点建议:
1、如果我
$db = new pdox;
$a = $db->query($sql1);
$b = $db->query($sql2);
print_r($a->fetchall());
print_r($b->fetchall());
可以吗?显然是不可以的,因为
$this->statement = $this->pdo->query($sql);
前一次的查询结果被后一次的覆盖了
2、事务(transaction)操作宜封装成整体,只传入一组 sql 而隐藏相关操作
3、没考虑使用存储过程
4、pdox 可直接继承于 pdo 而无需抄写 pdo 已有方法
谢谢你的宝贵意见,我仅仅是想让它操作更简单化一点。
1、像你举的这个例子,可有解决方法。
2、事务我很少用到,也不知道该怎样封装。如果你能指点一下更好。
3、存储过程也很少用到。
4、抄写pdo的方法,只是想在使用上和pdo无异样,方便直接写pdo的朋友直接使用此类。无须过多的学习。不像其它的数据库类,在学完原生的数据库操作后,又要再次学习一个新的数据库操作,可能它很简单,但学习也是要时间的。
总的来说还不错,但感觉上有些方法过于细琐,可以有必要的整合在一起
请问怎样整合会更简洁此呢??
封装的目的是为了简化操作,不常用的就没有不要封装了
class pdox extends pdo { //执行各种 sql 指令,并可通过参数 $param 进行扩展 function query($sql, $param=null) {} //查询并返回单条记录 function fetch($sql) {}; //查询并以数组方式返回多条记录 function fetchall($sql) {}}

数据库类只要有这三个方法就可满足绝大多数应用的需要了
由于是继承于 pdo,所以 pdo 原有的方法一个也不会少
@xuzuning
$db = new pdox;
$a = $db->query($sql1);
$b = $db->query($sql2);
print_r($a->fetchall());
print_r($b->fetchall());
这种写法的程序员应该不多吧?如果这样的话,可以考虑先放弃这种操作支持。如果我的类在效率或者并发上有问题的话,这才是大问题。
这个类内部是否有错误或者不妥之处??
各位大牛,请多多发言。
嵌套甚至递归的查询在读取分类树时是经常用到的,怎么能说不多呢?
嗯,做个留言板是不需要的
简单的插入数据:
$data = array(':title'=>$title, ':content'=>$content);
$pdo = new pdox;
$lastid = $pdo->insertbind('article', $data);
修改数据:
$data = array(':title'=>$title, ':id'=>$id);
$sql = 'update article set title=:title where id=:id';
$yesorno = $pdo->updatebind($sql, $data);
删除数据:
$data = array(':id'=>$id);
$sql = 'delete article where id=:id';
$yesorno = $pdo->deletebind($sql, $data);
最主要的是这种写法,可以省下很多的代码量。这才是我写此类的关键。
嵌套甚至递归的查询在读取分类树时是经常用到的,怎么能说不多呢?
嗯,做个留言板是不需要的
你说的这个还真是,在递归中需要使用。想使用此类的朋友要注意了。
我会尽量优化php代码或者使用其它方法替代递归,因为递归有些耗资源。
谢谢你提醒。
表示看的挺纠结的
表示看的挺纠结的
杂纠结了???
我觉得在操作数据时挺方便的呀
大牛们,继续呀。。
封装的目的是为了简化操作,不常用的就没有不要封装了
class pdox extends pdo { //执行各种 sql 指令,并可通过参数 $param 进行扩展 function query($sql, $param=null) {} //查询并返回单条记录 function fetch($sql) {}; //查询并以数组方式返回多条记录 function fetchall($sql) {}}

数据库类只要有这三个方法就可满足绝大多数应用的需要了
由于是继承于 pdo,所以 pdo 原有的方法一个也不会少
不知道版主可有更好的pdo操作类???
可否贡献一个。
不怎么样!
另外预处理部分没有封装
/** * 将数组转换为sql语句 * @param array $where 要生成的数组 * @param string $font 连接串。 */ final public function sqls($where, $font = ' and ') { # 先将bindcolumn存起来 $this->bindcolumn = array(); if (is_array($where)) { $sql = ''; foreach ($where as $key=>$val) { $sql .= $sql ? $font `$key` = :$key : `$key` = :$key; $this->bindcolumn = array($key, $val); } return ; } else { return $where; } }
其它类似信息

推荐信息