How do check if PDO object is connected properly inside a different class?

Posted by tgun926 on Stack Overflow See other posts from Stack Overflow or by tgun926
Published on 2013-11-02T21:38:08Z Indexed on 2013/11/02 21:53 UTC
Read the original article Hit count: 163

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql