繁体   English   中英

PHP PDO尝试捕获块未捕获

[英]PHP PDO try catch block not catching

此PHP PDO try catch块不会捕获任何错误。 这是为什么? 我做错了吗?

try {    
    $this->connect();      
    $preparedQuery = $this->pdo->prepare($sql);
    $this->pdo->beginTransaction();
    $preparedQuery->execute();
    $lastInsertId = $this->pdo->lastInsertId();
    $this->pdo->commit();
    return $lastInsertId;

} catch (PDOException $e) {
    $this->pdo->rollBack();
    return "error";
}

我正常运行了,我得到了这个错误

Fatal error: Call to a member function rollBack() on null 

PDO对象

private function connect() {
        $this->pdo = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database . '', $this->username, $this->password);
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

这是我的完整连接课程。

class ConnectionModel {

    private $host;
    private $username;
    private $password;
    private $database;
    private $pdo;

    function __construct() {
        $this->host = 'localhost'; // database server address
        $this->username = 'myuser'; //database server username;
        $this->password = 'mypass'; //database server password;
        $this->database = 'oms1'; //database name
    }

    private function connect() {
        $this->pdo = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database . '', $this->username, $this->password);
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }


    public function runQuery($sql) {

        try {

            $this->connect();
            $preparedQuery = $this->pdo->prepare($sql);
            $this->pdo->beginTransaction();
            $preparedQuery->execute();
            $lastInsertId = $this->pdo->lastInsertId();
            $this->pdo->commit();
            return $lastInsertId;
        } catch (PDOException $e) {
            $this->pdo->rollBack();
            return $e->getMessage();
        }
    }

}

正如@ Rizier123在他的评论中所述 ,您需要在PDO中设置错误模式:

$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

阅读有关PDO错误模式的更多信息

现在介绍为什么您没有得到您的错误。 您正在执行以下操作:

return 'error';

如果您没有收到实际的错误消息,那并不是完全有用的。

您想要的是获取实际的错误消息:

catch (PDOException $e) {
    $this->pdo->rollBack();
    echo $e->getMessage();
}

除此之外,如果您看不到存储在$sql实际SQL查询,就无法告诉您更多信息,如果您共享这些内容,我们可以提供更多帮助。

那么,您的问题是,您的连接没有任何异常。 因此,您必须这样做:

private function connect() {
    try {
        $this->pdo = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->database . '', $this->username, $this->password);
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}

现在,您在try and catch块中调用连接方法,因此该连接将引发Exception,然后从您的try and catch块中捕获该异常,该异常中也包含rollBack调用,但希望连接到成功的。

如果将连接调用放在try and catch块之外,则会出现未捕获的异常:

$this->connect();
try {

} catch(PDOException $e) {
    $this->pdo->rollBack();
    return $e->getMessage();
}

您需要将错误模式设置为基于异常:

$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

您的try / catch块已经捕获了每个错误。
在这条线

$this->connect();

它失败错误,然后您的代码中断捕获,所以,

$this->pdo

仍然为null则在catch块中,您尝试访问null object ,再次失败。
您可以检查if(isset($this->pdo))然后调用rollBack()函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM