1
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();
}
}
}
與數據庫連接類之間移動PDO連接不類
$conn = new dbConnection;
$workers = new Group("workers", $conn);
$workers->getGroupList();
後: 「連接成功」,我得到錯誤:
致命錯誤:調用第14行上的非對象的成員函數setAttribute()
我認爲這個類Group不會從dbConnection獲得$ pdo。我不知道我做錯了什麼。
他們沒有區別 – maszynka
查看編輯答案 – GBD
仍然是一樣的。 – maszynka