我正在尝试将它从好友表中获取好友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);
?>
<?php echo $row['username'];?>
}
} else {
?>
}
?>
最佳答案
准备好的语句使用参数,您应该使用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']
}