-2

I want to fetch some information from my mysql database in a class, so I'm passing in the PDO object into a __construct function, and working from there. However, what's an elegant way of checking to see if the PDO object was correctly created, and that the connection is open when the Table class is instantiated?

class Table{

    public function __construct(PDO $db, $week){

        try{
            $query = $db -> query ("SELECT * FROM `table1` where `day` = 'monday'");

        }
        catch(PDOExeption $e){
            echo 'error: '. $e->getMessage();
            //die();        
        }
    } 

}

I don't think this code does what I want.

4

1 回答 1

2

没有必要进行这种核查。

如果 PDO 对象创建不正确并且没有打开连接,则会抛出异常,因此脚本将在调用任何类的方法之前停止。

于 2013-11-02T21:40:57.820 回答