我試圖在向構造函數(PDO連接)傳遞參數時將多行讀取到實例中。我可以一次獲取一行,然後構建它們,但我正在尋找一種解決方案。將參數傳遞給構造函數(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();
}
}
爲什麼會這樣不正確? – artragis