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