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

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

封装了一个pdo的类,希望csdn的大牛们指点一下不足之处 封装了一个pdo的类,希望csdn的大牛们指点一下不足之处。 大师们,comeon! ?php /** *pdo封装类,目的是为了使用起来更简单方便 *modifydate:2014-07-01 */ classpdox{ private$pdo=null; public$statem
封装了一个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 = '';
【本文来自鸿网互联 (http://www.68idc.cn)】 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 已有方法
其它类似信息

推荐信息