一个PDO类

时间:2023-03-10 07:10:53
一个PDO类

下面是在网上借鉴的一个PDO类:

 <?php
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME; private $dbh;
private $error; private $stmt; /*
function __construct
*/
public function __construct(){
// set DSN
$dsn = 'mysql:host='.$this->host.';dbname='.$this->dbname;
//set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
//create a new PDO instanace
try{
$this->dbh = new PDO($dsn,$this->user,$this->pass,$options);
}
//catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
} /*
function query
*/
public function query($query){
$this->stmt = $this->dbh->prepare($query);
} /*
fucntion bindValue
*/
public function bind($param,$value,$type){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
} /*
functin execute
*/
public function execute(){
return $this->stmt->execute();
}
/*
获得结果集 关联数组
*/
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
} /* 获得单条数据*/
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
} public function rowCount(){
return $this->stmt->rowCount();
} public function lastInsertId(){
return $this->dbh->lastInsertId();
} public function beginTransaction(){
return $this->dbh->beginTransaction();
} public function endTransaction(){
return $this->dbh->commit();
} public function cancelTransaction(){
return $this->dbh->rollBack();
} public function debugDumpParams(){
return $this->stmt->debugDumpParams();
} }
?>

这个类的具体使用:

 try{
$database = new Database();
$database->beginTransaction();
$tm_startt = strtotime($_POST['time_start']);
$tm_end = strtotime($_POST['time_end']);
$database->query('insert into gua_user(user_name, user_nicheng, user_type, user_comment,user_created,user_end) values (:user_name, :user_nicheng, :user_type, :user_comment,:user_created,:user_end)');
$database->bind(':user_name', $_POST['username']);
$database->bind(':user_nicheng', $_POST['user_nick']);
$database->bind(':user_type', $_POST['user_type']);
$database->bind(':user_comment', $_POST['comment']);
$database->bind(':user_created', $tm_startt);
$database->bind(':user_end', $tm_end);
$database->execute();
//获得影响的行数
$rows = $database->rowCount();
//获得本条数据的id
$id = $database->lastInsertId();
if($rows<1){
throw new PDOexception('第一句sql语句执行失败!', '01');
}
//产生随机字符串 salt
$salt = $database->salt();
$password = md5($_POST['password'].$salt);
$database->query('insert into gua_salt(uid,salt,password) values (:uid,:salt,:password)');
$database->bind(':uid',$id);
$database->bind(':salt',$salt);
$database->bind(':password',$password);
$database->execute();
$rows = $database->rowCount();
if($rows<1){
throw new PDOexception('第二句sql语句执行失败!', '02');
}
$database->endTransaction();
}catch(PDOexception $e){
//如果有异常被抛出 则事务失败 执行事务回滚
$database->cancelTransaction();
//输出异常信息
echo $e->getCode().'-----'.$e->getMessage();
}

使用事物,注意表的类型要是innodb的。

参考网站:http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/