1

This question is related to this topic. I changed the syntax to one that was suggested so my code now looks like this:

method in class:

private $password;
private $user_id;
private $img_thumb;

public function getUserInfo(){
            $stmt = $this->dbh->prepare("SELECT user_id FROM oopforum_users WHERE username = ?");
            $stmt->bindParam(1, $this->post_data['username']);
            $stmt->execute();

            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                $password = $row['password'];
                $user_id = $row['user_id'];
                $img_thumb = $row['thumbnail'];
            }
            return array(
                'password' => $this->password,
                'id' => $this->user_id,
                'thumb' => $this->img_thumb
                );

        }

Calling program:

session_start();
require_once('init.php');

$username = trim($_POST['username']);
// create a new object
$login = new Auth($_POST, $dbh);

    if($login->validateLogin()){

        $_SESSION['loggedin'] = true;
        $list = $login->getUserInfo();


        $_SESSION['password'] = $list['password'];
        $_SESSION['id'] = $list['id'];
        $_SESSION['thumb'] = $list['img_thumb'];
}

None of the variables appear to be passed back to the calling program, have I missed something in the class where I need to explicitly state that they can be passed back?

I print_r()'d the $list array and it doesn't contain the three variables.

The three entries do exist in the database.

What am I doing wrong in getting the retrieved database entries back in the array in the calling program?

Community
  • 1
  • 1
crmepham
  • 4,676
  • 19
  • 80
  • 155

1 Answers1

2

On your while loop, assign the variables to current object:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $this->password  = $row['password'];
  $this->user_id   = $row['user_id'];
  $this->img_thumb = $row['thumbnail'];
}

Update: And you missed some fields in your SQL:

SELECT user_id, password, img_thumb FROM oopforum_users WHERE username = ?
flowfree
  • 16,356
  • 12
  • 52
  • 76