PDO connection doesn’t move between classes

class Group extends dbConnection { //Class for group, for ex. employe and employers.
    public $name;  // Name of group
    public $pdo;

    public function __construct ($name, dbConnection $pdo) {
    $this->pdo = $database->pdo;
    }

    public function getGroupList() {  
        try //                       FAULTY LINE BELOW \/\/\/\/\/\/\/
        {
          $pdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //to catch exceptions

          $stmt = $pdo -> query('SELECT id, Name, Skills FROM '.$this->name); //sql query with group name
          $list = $stmt->fetchAll(PDO::FETCH_NUM); //fetch statement into array
          $stmt -> closeCursor();
          unset($stmt);
          return var_dump($list); //gives pure data
        }
        catch(PDOException $e) 
        {
          return 'There was some error: ' . $e->getMessage();
        }
    }

}

And dbConnect class:

  class dbConnection {
    public $pdo;
    public function __construct () {
        $this->dbConnect();
    }
    public function dbConnect () {
        if((count($_POST) == 6)&&($_GET['a'] == "connect")) {
            $host = $_POST['host'];
            $port = $_POST['port'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $database = $_POST['database']; 
            try{
                    $pdo = new PDO('mysql:host='.$host.';dbname='.$database.';port='.$port, $username, $password );
                    echo 'Connection successful!';
                    return $pdo;
            }
            catch(PDOException $e){
                    echo 'Error: ' . $e->getMessage();
            }   
        }
    }
}

And execution:

$conn = new dbConnection;
$workers = new Group("workers", $conn);
$workers->getGroupList();

After: “Connection successful” I get error:

Fatal error: Call to a member function setAttribute() on a non-object on line 14

I think that class Group just doesnt get $pdo from dbConnection. I don’t know what I’m doing wrong.

Leave a Reply

*

Hire Me
Follow Me!
Search
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.
Categories