Reputation: 739
I have a function which retrieves user information and stores it in variables like so within a class:
public function UserInformation() {
$query = "SELECT * FROM users WHERE username = '$this->user'";
try {
$stmt = $this->db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$rows = $stmt->fetch();
$email = $rows['email'];
$username = $rows['username'];
}
How would I then display a single variable from that function? I've tried echo $retrieveInfo->UserInformation->username;
with no success, how would I do this?
Upvotes: 0
Views: 58
Reputation: 254886
You have defined local variables, that to be lost as soon as function ends its execution.
The more correct way of doing what you want would be to return the data from the function like:
$rows = $stmt->fetch();
return $rows;
And call the method like
$rows = $yourObject->UserInformation();
Or, if the object represents a particular user you could store the data retrieved in an instance members like:
$this->email = $rows['email'];
and then access them
$yourObject->email
This would work as well while the former is what I would prefer (I don't know the whole task though)
Upvotes: 1