简体   繁体   中英

PHP PDO try catch block not catching

This PHP PDO try catch block doesn't catch any error. 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

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:

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

Read more about PDO Error Modes

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.

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.

If you put your connection call outside of the try and catch block you would get an uncaught Exception:

$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.
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.
you can check if(isset($this->pdo)) then call rollBack() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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