为什么此查询应该显示2时才显示一个结果?

时间:2018-12-25 04:33:42

标签: php mysqli

num_rows显示2行结果,而while循环仅显示一行。
如何使用while循环显示两行?

$getTheImage = $mysqli->prepare("SELECT AES_DECRYPT(USERNAME,MEM_KEY) AS USERNAME, AVATAR FROM MEMBERS WHERE DEV_ADD=AES_ENCRYPT('".$dipaddB."',MEM_KEY)");
$getTheImage->execute();
$getTheImage->bind_result($username, $avatar);
$getTheImage->store_result();


echo $getTheImage->num_rows;
if($avatar == ''){$ava = 'styles/imagesio/anon_small.png';}else{$ava =  $avatar;}
while($getTheImage->fetch()){ 
$LoggedAccounts  = "<div class=\"boxHead\">Auto Login Accounts</div>";
$LoggedAccounts .= "<div class=\"resCon\"><button class=\"quickButton\" onclick=\"loggmein();\" id=\"".$username."\"><img class=\"avatQuick\" src=\"".$ava."\" alt=\"ioguy\"><div class=\"usrClass\">".$username."</div></button></div>";
}

$getTheImage->close();

2 个答案:

答案 0 :(得分:2)

您每次迭代都会覆盖。将初始化移到while之前。

您还滥用了准备好的陈述。您应该绑定值。

$getTheImage = $mysqli->prepare("SELECT AES_DECRYPT(USERNAME,MEM_KEY) AS USERNAME, AVATAR FROM MEMBERS WHERE DEV_ADD=AES_ENCRYPT(?, MEM_KEY)");
$getTheImage->bind_param('s', $dipaddB);
$getTheImage->execute();
$getTheImage->bind_result($username, $avatar);
$getTheImage->store_result();


echo $getTheImage->num_rows;
$ava = ($avatar == '') ? 'styles/imagesio/anon_small.png' : $avatar;
$LoggedAccounts = '';
while($getTheImage->fetch()){ 
     $LoggedAccounts .= "<div class=\"boxHead\">Auto Login Accounts</div>";
     $LoggedAccounts .= "<div class=\"resCon\"><button class=\"quickButton\" onclick=\"loggmein();\" id=\"".$username."\"><img class=\"avatQuick\" src=\"".$ava."\" alt=\"ioguy\"><div class=\"usrClass\">".$username."</div></button></div>";
}
$getTheImage->close();

答案 1 :(得分:1)

每次循环执行时,

$ LoggedAccounts将被覆盖。试试这个。

while($getTheImage->fetch()){ 
    $LoggedAccounts .= "<div class=\"boxHead\">Auto Login Accounts</div>";
    $LoggedAccounts .= "<div class=\"resCon\"><button class=\"quickButton\" onclick=\"loggmein();\" id=\"".$username."\"><img class=\"avatQuick\" src=\"".$ava."\" alt=\"ioguy\"><div class=\"usrClass\">".$username."</div></button></div>";
}