此问题与this主题相关。我将语法更改为建议的语法,因此我的代码现在看起来像这样:
课堂上的方法:
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
);
}
致电计划:
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'];
}
没有任何变量似乎被传递回调用程序,我是否遗漏了类中需要明确声明可以传回的内容?
我print_r()
'$list
数组,它不包含三个变量。
数据库中确实存在三个条目。
在调用程序中将检索到的数据库条目返回到数组中我做错了什么?
答案 0 :(得分:2)
在while循环中,将变量分配给当前对象:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$this->password = $row['password'];
$this->user_id = $row['user_id'];
$this->img_thumb = $row['thumbnail'];
}
更新:您错过了SQL中的一些字段:
SELECT user_id, password, img_thumb FROM oopforum_users WHERE username = ?