0

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?

1
  • You could set up an output as an array. So $output['username'] = $rows['username'];. Then you just call UserInformation['username'] Commented Aug 5, 2013 at 23:28

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.