我试图将它从朋友表中获取朋友ID,然后从用户表中获取朋友信息。我尝试过使用foreach但没有运气。
这就是我现在所拥有的,它只回应一位朋友,而不是我在桌子上的三位朋友。关于如何解决这个问题的任何想法?也许我没有正确地使用它们?先感谢您!
<?php
//Gets users information from users.
$stmt = $DB_con->prepare('SELECT friendsid FROM friends WHERE userid='.$_SESSION['user']['id']);
$stmt->execute();
if($stmt->rowCount() > 0) {
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
} else {
$friendsid = $row['friendsid'];
}
$stmt = $DB_con->prepare('SELECT username,userprofile,status FROM users WHERE id='.$friendsid);
$stmt->execute();
if($stmt->rowCount() > 0) {
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<div class="row">
<div class="col-sm-3">
<div class="well">
<h4><strong><?php echo $row['username'];?></strong></h4>
<img src="images/profile/<?php echo $userprofile;?>" class="img-circle" height="70" width="70" alt="Avatar">
</div>
</div>
<div class="col-sm-9">
<div class="well">
<h3 class="text-left"><?php echo $row['status'];?></h3>
</div>
</div>
</div>
<?php
}
} else {
?>
<?php
}
?>
答案 0 :(得分:0)
预备语句使用参数,您应该使用JOIN。
$stmt = $DB_con->prepare('SELECT u.* FROM users AS u JOIN friends AS f ON u.id = f.friendsid WHERE f.userid = :user_id');
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
// Do whatever you want with $row['id'], $row['username'], $row['userprofile']
}