关于 Laravel:Laravel – 无法捕获 PDO 异常 | 珊瑚贝

Laravel – can’t catch PDO exceptions


我有这个:

1
2
    $this->db = DB::connection()->getPdo();
    $this->db->setAttribute(\\PDO::ATTR_ERRMODE, \\PDO::ERRMODE_EXCEPTION);

还有一个 try…catch 块,它似乎不做任何捕获:

1
2
3
4
5
6
7
    try {
        $this->SQL->execute($this->values);
        $this->insert_count++;
    } catch (PDOexception $e) {
        $this->errors[] = ‘PDO error: ‘ . $e->getmessage();
        $this->error_count++;
    }

…因为脚本仍然在 PDO 异常上退出:

[PDOException] SQLSTATE[23000]: Integrity constraint violation:
1062 Duplicate entry ’14’ for key ‘laptops_asset_tag_unique’

那么我该如何捕捉/处理 PDO 错误呢?


你有没有为你的异常命名空间?

您必须将 PDOException 导入您的类才能使用它。
在 php 文件的顶部添加以下语句

1
use PDOException;
  • 这是另一个将引发的错误:CLASS NOT FOUND ‘PDOException’ …(如果存在)


来源:https://www.codenong.com/41074604/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8675.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?