How can I fetch selected data to an object and retrieve the selected columns name with PDO?
Now, I’m fetching to a Member object in a loop, and manually add each column to the object by accessing it from the “fetch array”. Is there a way to automatically get the column names that’s been seleced?
$sql = "SELECT member.name, member.email FROM member WHERE id = :id";
$param = array(':id' => $id);
$stmt = $this->m_db->select($sql, $param);
$ret = new MemberArray();
while ($f = $stmt->fetch()) {
$member = new Member($f['name'], $f['email']);
$ret->add($member);
}
return $ret;
I found the solution in PDOStatement::bindColumn and PDO::FETCH_OBJ