如何对PDO使用try-catch块 [英] How to use try-catch block for PDO

查看:147
本文介绍了如何对PDO使用try-catch块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用try catch块时,处理PDO错误的正确方法是什么?

What is a decent way to handle PDO error when using try catch block?

目前我有这样的东西:

BlogModel.php

   class BlogModel extends Model {
       public function save($id, $value) {
         $stmt = $this->getDb()->prepare('UPDATE setting SET name = :name WHERE id = :id');
         $stmt->bindParam(':id', $id);
         $stmt->bindParam(':name', $values);
         return ($stmt->execute() !== false) ? $id : false;
       }
   }

因此,在控制器 BlogController.php 中,我将执行以下操作:

So, in the controller BlogController.php, I would do something like this:

<?php
class Blog extends Controller {

    public function comments()
    {
        $data = array();
        $model = new BlogModel;

        if ($model->save(2,'test')) {
                $data['result']['message'] = 'Settings saved';
                $data['result']['status'] = 'success';
        } else {
                $data['result']['message'] = 'Could not save the settings';
                $data['result']['status'] = 'error';
        }

        $view = new View("view.php", $data)
        $view->render();
    }
}
?>

这是我使用if条件处理PDO错误的方式.将其转换为try catch块的体面方法是什么?我不想一直对变量($data['result']['message'] $data['result']['status'])进行编码.

This is the way I handle PDO error using if conditions. What is the decent way to translate this into try catch block? I don't want to code the variables ($data['result']['message'] $data['result']['status']) all the time.

是否可以通过某种方式在catch块中添加抛出异常"?

Is possible to add "throw exception" in the catch block somehow?

如果控制器中有很多try catch块,它看起来会很杂乱..对吗?

If there is a lot of try catch blocks in the controller, it going to look messy.. right?

推荐答案

这里的答案都不对.但实际上,这三者的总和才是真正的答案. 您绝对应该设置

None of the answers here are wrong. But actually all three combined are the real answer. You should definitely set

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

Cerad 所说.

从现在开始,关于类型库的所有问题都会通过PDOException类型的异常抛出.您不必像 ladar 所说的那样扔自己的Exception,因为它没有用.只需使用 ladar 代码并将其转换为

From now on every single issue about anything regarding database is thrown via exception of type PDOException. You just don't have to throw your own Exception as said by ladar because it's useless. Just take the ladar code and convert it into

 ...
 $data = array();
 $model = new BlogModel;

    try{
      $model->save(2,'test');
      $data['result']['message'] = 'Settings saved';
      $data['result']['status'] = 'success';
    }catch(PDOException $e){
        $data['result']['message'] = 'Could not save the settings';
        $data['result']['status'] = 'error';
    }

也不要自己扔东西.

然后,调试PDO查询的一种非常不错的方法是使用 Basic 链接的catch脚本,您可以找到

Then a very nice way for debugging PDO queries is using the catch script linked by Basic that you can find here once again.

将这些东西结合起来,您将拥有一种灵活,干净且易于调试的方式来捕获可能出现的所有错误.

Combining this things togheter you'll have a flexible, clean and easy-debug way to catch all the errors that could come.

这篇关于如何对PDO使用try-catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆