I'm trying to push objects containing elements from a sql result set into an array. My code is like:
$data = array();
$sql = "SELECT id,type,name,username FROM users";
foreach ($conn->query($sql) as $row) {
$this->set_id($row['id']);
$this->set_type($row['type']);
$this->set_username($row['username']);
$this->set_password($row['password']);
$data[] = $this;
}
My resultset is correct but I get my array cells overwritten with values from the last recordset after the foreach loop ends. For example, if I have these results {1,'type1','user','pass'}, {2,'type2','foo','bar'}, when I print_r my $data array ouside of the loop, I get only the second resultset repeated twice. What am I doing wrong?