封装了一个PDO的类,希望CSDN的大牛们指点一下不足之处 封装了一个PDO的类,希望CSDN的大牛们指点一下不足之处。 大师们,comeon! ?php /** *PDO封装类,目的是为了使用起来更简单方便 *modifyDate:2014-07-01 */ classPDOX{ private$pdo=null; public$statem
封装了一个PDO的类,希望CSDN的大牛们指点一下不足之处
<?php<br />
/**<br />
* PDO封装类,目的是为了使用起来更简单方便<br />
* modify Date: 2014-07-01<br />
*/<br />
class PDOX {<br />
private $pdo = null;<br />
<br />
public $statement = null;<br />
<br />
public $options = array(<br />
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ",<br />
);<br />
<br />
public function __construct($dsn, $user = '', $pass = '', $persistent = false, $charset = "utf8"){<br />
$this->options[PDO::MYSQL_ATTR_INIT_COMMAND] .= $charset;<br />
if($persistent){<br />
$this->options[PDO::ATTR_PERSISTENT] = true;<br />
}<br />
$this->pdo = new PDO($dsn, $user, $pass, $this->options);<br />
}<br />
/**<br />
全局属性设置,包括:列名格式和错误提示类型 可以使用数字也能直接使用参数<br />
*/<br />
public function setAttr($param, $val = ''){<br />
if(is_array($param)){<br />
foreach($param as $key => $val){<br />
$this->pdo->setAttribute($key, $val);<br />
}<br />
}else{<br />
if($val != '' ){<br />
$this->pdo->setAttribute($param, $val);<br />
}else{<br />
return false;<br />
}<br />
}<br />
}<br />
/**<br />
生成一个编译好的sql语句模版 你可以使用 ? :name 的形式<br />
返回一个statement对象<br />
*/<br />
public function prepare($sql){<br />
if(empty($sql)){<br />
return false;<br />
}<br />
$this->statement = $this->pdo->prepare($sql);<br />
return $this->statement;<br />
}<br />
/**<br />
执行Sql语句,一般用于 增、删、更新或者设置 返回影响的行数<br />
*/<br />
public function exec($sql){<br />
if(empty($sql)){<br />
return false;<br />
}<br />
try{<br />
return $this->pdo->exec($sql);<br />
}catch(Exception $e){<br />
return $e->getMessage();<br />
}<br />
}<br />
/**<br />
执行有返回值的查询,返回PDOStatement 可以通过链式操作,可以通过这个类封装的操作获取数据<br />
*/<br />
public function query($sql){<br />
if(empty($sql)){<br />
return false;<br />
}<br />
$this->statement = $this->pdo->query($sql);<br />
return $this->statement;<br />
}<br />
/**<br />
开启事务<br />
*/<br />
public function beginTransaction(){<br />
return $this->pdo->beginTransaction();<br />
}<br />
/**<br />
提交事务<br />
*/<br />
public function commit(){<br />
return $this->pdo->commit();<br />
}<br />
/**<br />
事务回滚<br />
*/<br />
public function rollBack(){<br />
return $this->pdo->rollBack();<br />
}<br />
<br />
public function lastInertId(){<br />
return $this->pdo->lastInsertId();<br />
}<br />
<br />
<br />
//** PDOStatement 类操作封装 **//<br />
<br />
/**<br />
让模版执行SQL语句,1、执行编译好的 2、在执行时编译<br />
*/<br />
public function execute($param = ""){<br />
if(is_array($param)){<br />
try{<br />
return $this->statement->execute($param);<br />
}catch (Exception $e){<br />
//return $this->errorInfo();<br />
return $e->getMessage();<br />
}<br />
}else{<br />
try{<br />
return $this->statement->execute();<br />
}catch(Exception $e){<br />
/* 返回的错误信息格式<br />
[0] => 42S22<br />
[1] => 1054<br />
[2] => Unknown column 'col' in 'field list'<br />
return $this->errorInfo();<br />
*/<br />
return $e->getMessage();<br />
}<br />
}<br />
}<br />
<br />
/**<br />
参数1说明:<br />
PDO::FETCH_BOTH 也是默认的,两者都有(索引,关联)<br />
PDO::FETCH_ASSOC 关联数组<br />
PDO::FETCH_NUM 索引<br />
PDO::FETCH_OBJ 对象<br />
PDO::FETCH_LAZY 对象 会附带queryString查询SQL语句<br />
PDO::FETCH_BOUND 如果设置了bindColumn,则使用该参数<br />
*/<br />
public function fetch($fetch_type = PDO::FETCH_ASSOC){<br />
if(is_object($this->statement)){<br />
return $this->statement->fetch($fetch_type);<br />
}<br />
return false;<br />
}<br />
/**<br />
参数1说明:<br />
PDO::FETCH_BOTH 也是默认的,两者都有(索引,关联)<br />
PDO::FETCH_ASSOC 关联数组<br />
PDO::FETCH_NUM 索引<br />
PDO::FETCH_OBJ 对象<br />
PDO::FETCH_COLUMN 指定列 参数2可以指定要获取的列<br />
PDO::FETCH_CLASS 指定自己定义的类<br />
PDO::FETCH_FUNC 自定义类 处理返回的数据<br />
PDO_FETCH_BOUND 如果你需要设置bindColumn,则使用该参数<br />
参数2说明:<br />
给定要处理这个结果的类或函数<br />
*/<br />
public function fetchAll($fetch_type = PDO::FETCH_ASSOC, $handle = ''){<br />
if(empty($handle)){<br />
return $this->statement->fetchAll($fetch_type);<br />
}<br />
return $this->statement->fetchAll($fetch_type, $handle);<br />
}<br />
/**<br />
以对象形式返回 结果 跟fetch(PDO::FETCH_OBJ)一样<br />
*/<br />
public function fetchObject($class_name){<br />
if(empty($clss_name)){<br />
return $this->statement->fetchObject();<br />
}<br />
return $this->statement->fetchObject($class_name);<br />
}<br />
<br />
public function fetchColumn($intColumn = 0){<br />
return $this->statement->fetchColumn($intColumn);<br />
}<br />
<br />
/**<br />
public function bindColumn($array=array(),$type=EXTR_OVERWRITE){<br />
if(count($array)>0){<br />
extract($array,$type);<br />
}<br />
//$this->statement->bindColumn()<br />
}<br />
*/<br />
<br />
/**<br />
以引用的方式绑定变量到占位符(可以只执行一次prepare,执行多次bindParam达到重复使用的效果)<br />
*/<br />
public function bindParam($parameter, $variable, $data_type = 'STR', $length = 0){<br />
switch ($data_type){<br />
case 'STR':<br />
$data_type = PDO::PARAM_STR;<br />
break;<br />
case 'INT':<br />
$data_type = PDO::PARAM_INT;<br />
break;<br />
default :<br />
$data_type = '';<br />
【本文来自鸿网互联 (http://www.68idc.cn)】 break;<br />
}<br />
return $this->statement->bindParam($parameter, $variable, $data_type, $length);<br />
}<br />
<br />
/**<br />
返回statement记录集的行数<br />
*/<br />
public function rowCount(){<br />
return $this->statement->rowCount();<br />
}<br />
public function count(){<br />
return $this->statement->rowCount();<br />
}<br />
public function columnCount(){<br />
$this->statement->execute();<br />
return $this->statement->columnCount();<br />
}<br />
public function getColumnMeta($intColumn){<br />
return $this->statement->getColumnMeta($intColumn);<br />
}<br />
<br />
<br />
/**<br />
关闭<br />
*/<br />
public function close(){<br />
return $this->statement->closeCursor();<br />
}<br />
<br />
public function closeCursor(){<br />
return $this->statement->closeCursor();<br />
}<br />
/**<br />
返回错误信息也包括错误号<br />
*/<br />
private function errorInfo(){<br />
return $this->statement->errorInfo();<br />
}<br />
/**<br />
返回错误号<br />
*/<br />
private function errorCode(){<br />
return $this->statement->errorCode();<br />
}<br />
<br />
<br />
<br />
//简化操作<br />
public function insert($table, $data){<br />
if(!is_array($data)){<br />
return false;<br />
}<br />
$cols = array();<br />
$vals = array();<br />
foreach($data as $key => $val){<br />
$cols[] = $key;<br />
$vals[] = "'" . $val . "'";<br />
}<br />
$sql = "INSERT INTO {$table} (";<br />
$sql .= implode(",", $cols) . ") VALUES ("; <br />
$sql .= implode(",", $vals) . ")";<br />
return $this->exec($sql);<br />
}<br />
public function insertBind($table, $arrayData){<br />
if(!is_array($arrayData)){<br />
return false;<br />
}<br />
$vals = array_keys($arrayData);<br />
$cols = array();<br />
/*<br />
$arrayobject = new ArrayObject( $arrayData );<br />
$iterator = $arrayobject->getIterator();<br />
while($iterator->valid()) {<br />
$vals[] = ':' . $iterator->key() . '';<br />
$iterator->next();<br />
}<br />
*/<br />
$c = implode('', $vals);<br />
$cols = array_filter(explode(':', $c));<br />
<br />
$sql = "INSERT INTO {$table} (";<br />
$sql .= implode(",", $cols) . ") VALUES ("; <br />
$sql .= implode(",", $vals) . ")";<br />
<br />
$this->statement = $this->pdo->prepare($sql);<br />
$this->statement->execute($arrayData);<br />
return $this->statement->rowCount();<br />
}<br />
<br />
public function update($table, $data, $where){<br />
if(!is_array($data)){<br />
return false;<br />
}<br />
$set = array();<br />
foreach($data as $key => $val){<br />
$set[] = $key . "='" . $val . "'";<br />
}<br />
$sql = "UPDATE {$table} SET ";<br />
$sql .= implode(",", $set);<br />
$sql .= " WHERE " . $where;<br />
return $this->exec($sql);<br />
}<br />
public function updateBind($sql, $arrayWhere){<br />
if(empty($sql) || !is_array($arrayWhere)){<br />
return false;<br />
}<br />
$this->statement = $this->pdo->prepare($sql);<br />
$this->statement->execute($arrayWhere);<br />
return $this->statement->rowCount();<br />
}<br />
<br />
<br />
public function delete($table, $where){<br />
if(empty($table) || empty($where)){<br />
return false;<br />
} <br />
$sql = "DELETE FROM {$table} WHERE " . $where;<br />
return $this->exec($sql);<br />
}<br />
public function deleteBind($sql, $arrayWhere){<br />
if(empty($sql) || !is_array($arrayWhere)){<br />
return false;<br />
}<br />
$this->statement = $this->pdo->prepare($sql);<br />
$this->statement->execute($arrayWhere);<br />
return $this->statement->rowCount();<br />
}<br />
}<br />
?>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号