我对PHP比较陌生,我很难从类中获取正确的输出。我在一个准备好的语句中使用SELECT *,试图拉回一行WHERE登录状态设置为。我目前使用的Thge代码创建一个数组,但是如果有多个用户登录,则第二个结果显示为写在第一行,只允许我显示一行。
通常我会用2d数组来完成这样的任务,其中每一行都是一个包含键值对数组的数组,但是我很难让它工作。
方法如下:
public function getUserByLoginStatus($loginStatus) {
$session=new mysqli(self::SERVERNAME,self::USERNAME,self::DBPASS);
mysqli_select_db($session,"users");
$statement=$session->prepare("SELECT * FROM `logins` WHERE `loginstatus`= ? ;");
$statement->bind_param('s', $this->loginStatus);
$statement->execute();
$statement->store_result();
$statement->bind_result($userId,$userHandle,$eMail,$password,$gender,
$loginStatus,$currentSess,$currentIpv4,$currentIpv6,
$lastActivity,$lastLoginDate,$lastLoginTime);
$numberOfRows=$statement->num_rows;
if($numberOfRows < 1)
{
$errorMessage="No users currently online";
$statement->close();
$session->close();
return $errorMessage;
}
else
{
while($statement->fetch())
{
$returnArr=['userid' => $userId, 'userhandle' => $userHandle, 'email' => $eMail,
'password' => $password, 'gender' => $gender, 'loginstatus' => $loginStatus,
'currentsess' => $currentSess, 'currentipv4' => $currentIpv4, 'currentipv6' => $currentIpv6,
'lastactivity' => $lastActivity, 'lastlogindate' => $lastLoginDate, 'lastlogintime' => $lastLoginTime];
}
$statement->close();
$session->close();
return $returnArr;
}
}
如果表格col设置为“out”或“deactivated”,则会显示正确的错误消息。如果任何1个用户设置为“in”,我可以查看字段中的数据。如果&gt; 1个用户在线,则只显示最后一个用户(我相信会覆盖)。
以下是我目前试图打印出来的方法:
答案 0 :(得分:1)
尝试关注while循环
while($statement->fetch())
{
$returnArr[]=['userid' => $userId, 'userhandle' => $userHandle, 'email' => $eMail, 'password' => $password, 'gender' => $gender, 'loginstatus' => $loginStatus, 'currentsess' => $currentSess, 'currentipv4' => $currentIpv4, 'currentipv6' => $currentIpv6, 'lastactivity' => $lastActivity, 'lastlogindate' => $lastLoginDate, 'lastlogintime' => $lastLoginTime];
}