$stmt = $dbh->prepare("SELECT name, age FROM user"); $stmt->setFetchMode(PDO::FETCH_ASSOC); $results = $stmt->fetchAll();
class User { public $name; public $age; } $stmt = $dbh->prepare("SELECT name, age FROM user"); $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $results = $stmt->fetchAll();This code sets the fetch mode to FETCH_CLASS, which means that each row retrieved will be returned as an instance of the specified class (in this case, the User class). This method is part of the PDO package library in PHP.