我试图在将参数传递给构造函数(PDO 连接)时将多行提取到实例中。我可以一次取一行然后构造它们,但我正在寻找一种单行解决方案。
class Member {
// PDO connection
public $conn;
public $Members_ID;
public $Members_FirstName;
public $Members_LastName;
function __construct($conn){
$this->conn= $conn;
}
// Returns an array of Members with the email address that is given
// (Multiple members can have the same email)
public static function getMembersFromEmailAddress($emailAddress, $conn){
try
{
$result = $conn->query("SELECT * FROM members WHERE Members_Email ='". $emailAddress ."'");
//HERE I WANT MEMBERS TO BE AN ARRAY OF MEMBER OBJECTS
$members = $result->fetch(PDO::FETCH_INTO, new Member($conn));
return $members
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}