简体   繁体   English

PHP PDO尝试捕获块未捕获

[英]PHP PDO try catch block not catching

This PHP PDO try catch block doesn't catch any error. 此PHP PDO try catch块不会捕获任何错误。 Why is that? 这是为什么? did I make a mistake? 我做错了吗?

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";
}

I ran it normally and i get this eror 我正常运行了,我得到了这个错误

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

PDO object 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);
    }

Here's my full connection class. 这是我的完整连接课程。

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();
        }
    }

}

As @Rizier123 stated in his comment , you need to set your error mode on in PDO: 正如@ Rizier123在他的评论中所述 ,您需要在PDO中设置错误模式:

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

Read more about PDO Error Modes 阅读有关PDO错误模式的更多信息

Now to why you don't get your errors. 现在介绍为什么您没有得到您的错误。 You are doing the following in your catch: 您正在执行以下操作:

return 'error';

That isn't completely useful if you don't get the actual error message. 如果您没有收到实际的错误消息,那并不是完全有用的。

What you want is to fetch the actual error message: 您想要的是获取实际的错误消息:

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

Other than that, we can't tell you much more without seeing your actual SQL Query stored in $sql , if you share that, we can help more. 除此之外,如果您看不到存储在$sql实际SQL查询,就无法告诉您更多信息,如果您共享这些内容,我们可以提供更多帮助。

Well your problem is, that you don't catch any exceptions for your connection. 那么,您的问题是,您的连接没有任何异常。 So you would have to do this: 因此,您必须这样做:

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();
    }
}

Right now your call your connection method in a try and catch block, so in your method the connection will throw an Exception, which then gets caught from your try and catch block, which also has the rollBack call in it, but expects the connection to be successful. 现在,您在try and catch块中调用连接方法,因此该连接将引发Exception,然后从您的try and catch块中捕获该异常,该异常中也包含rollBack调用,但希望连接到成功的。

If you put your connection call outside of the try and catch block you would get an uncaught Exception: 如果将连接调用放在try and catch块之外,则会出现未捕获的异常:

$this->connect();
try {

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

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

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

your try/catch block already catch every error. 您的try / catch块已经捕获了每个错误。
in this line 在这条线

$this->connect();

it fail error, then your code break to catch, so, 它失败错误,然后您的代码中断捕获,所以,

$this->pdo

is still null then, in catch block, you try to access null object , it fail error again. 仍然为null则在catch块中,您尝试访问null object ,再次失败。
you can check if(isset($this->pdo)) then call rollBack() function. 您可以检查if(isset($this->pdo))然后调用rollBack()函数。

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

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