我有这个代码用于从数据库中提取所有用户。 此代码有效,并显示来自DB的所有用户
<?php
$sqlsel = "SELECT picture,username,email,configured,gender FROM members ORDER BY username";
$stmt = $mysqli->prepare($sqlsel);
$stmt->execute();
$stmt->bind_result($profpicture,$uname,$email,$confred,$gender);
while($stmt->fetch())
{
if(empty($profpicture)){
echo "<img src='profile_pictures/public_icon.png' width='50' height='50'><br>";}else {
echo " <div class='img'><img src='profile_pictures/".$profpicture."' alt='Profile Pic'></div><br>";
}
echo "<b>Email:</b> ".$email."<br> ";
if($gender == Woman){
$gen = her;
}
else{
$gen = his;
}
echo "<b>Username: </b>".$uname." </p> ";
if(empty($confred)){
echo "<font color='#FF4747'>".$uname." has not configured ".$gen." account yet.</font>";
} else{
echo "<form action='".$uname.".php'>
<input type='submit' class='buttondiv' value='Visit ".$uname."'s Profile'>
</form>";
}
echo "<hr class='hr' />";
}
?>
所以我做的几乎一样,但有帖子。它应该从数据库中提取所有帖子,但它只显示第一个...为什么?
<?php
$selectposts = "SELECT postby,posttxt,time FROM posts ORDER BY time";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby,$posttxt,$posttime);
while($stmt->fetch()){
$stmt->close();
$stmt = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$stmt->bind_param('s', $postby);
$stmt->execute();
$stmt->bind_result($picture);
while ($stmt->fetch()){
if(empty($picture)){
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/".$picture."' alt='Profile Picture'>";
}
echo
"<p><center><div class='postdv'>
<b>".$profpicturefromdb."
<h3>".$postby."</h3></b><font color='#000'>
[ ".$posttime." ]</font><hr class='hr'>
<font color='#000'>".$posttxt."</font><hr class='hr'>
<form method='POST'>
<input type='submit' name='voteup' class='voteup' value='Up'>
<input type='submit' name='votedown' class='votedown' value='Down'>
</form>
</center>
</div>";
}
}
?>
由于
答案 0 :(得分:0)
这是因为您在$ stmt变量中重写了第一个查询结果。试试这个:
<?php
$selectposts = "SELECT postby,posttxt,time FROM posts ORDER BY time";
$stmt = $mysqli->prepare($selectposts);
$stmt->execute();
$stmt->bind_result($postby, $posttxt, $posttime);
while ($stmt->fetch()) {
$stmt1 = $mysqli->prepare("SELECT picture FROM members WHERE username = ?");
$stmt1->bind_param('s', $postby);
$stmt1->execute();
$stmt1->bind_result($picture);
while ($stmt1->fetch()) {
if (empty($picture)) {
$profpicturefromdb = " <img src='profile_pictures/public_icon.png' width='25' height='25' class='fxdmimg'>";
} else {
$profpicturefromdb = " <img width='25' class='fxdmimg' height='25' src='profile_pictures/" . $picture . "' alt='Profile Picture'>";
}
echo "<p><center><div class='postdv'>
<b>" . $profpicturefromdb . "
<h3>" . $postby . "</h3></b><font color='#000'>
[ " . $posttime . " ]</font><hr class='hr'>
<font color='#000'>" . $posttxt . "</font><hr class='hr'>
<form method='POST'>
<input type='submit' name='voteup' class='voteup' value='Up'>
<input type='submit' name='votedown' class='votedown' value='Down'>
</form>
</center>
</div>";
}
}
$stmt->close();
?>