0
我有一個PDO數據庫類,我有點新的PDO,所以我不知道如何去返回錯誤,所以我可以調試,如果出現問題。我只想知道捕捉錯誤並將它們返回給請求的最佳方法。這裏是我的數據庫類:PDO數據庫類,返回錯誤
class database {
private $dbh;
private $stmt;
public function __construct($user, $pass, $dbname) {
$this->dbh = new PDO(
"mysql:host=localhost;dbname=$dbname",
$user,
$pass,
array(PDO::ATTR_PERSISTENT => true)
);
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
return $this;
}
public function bind($pos, $value, $type = null) {
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($pos, $value, $type);
return $this;
}
public function execute() {
return $this->stmt->execute();
}
public function resultset() {
$this->execute();
return $this->stmt->fetchAll();
}
public function single() {
$this->execute();
return $this->stmt->fetch();
}
}
感謝
相關:http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo – 2012-02-24 17:47:01
通過佩卡發佈的鏈接,他有答案 – 2012-02-24 19:03:16