我有一个从数据库中提取行的朋友页面,我只想显示当前用户的朋友,但它只从查询中返回当前用户。
数据库表:
当前代码:
function getFriends($user_id, $sqli_con) {
$user_id = mysqli_escape_string($sqli_con, strip_tags($user_id));
$results = $sqli_con->query("SELECT * FROM friends WHERE receiver_id = '$user_id' AND accepted = '1' OR sender_id = '$user_id' AND accepted = '1'");
if($results->num_rows > 0) {
while($row = $results->fetch_array(MYSQLI_ASSOC)) {
if($row['receiver_id'] === $user_id || $row['sender_id'] === $user_id) {
} else {
$username_stmt = $sqli_con->prepare("SELECT id, username FROM members WHERE id = {$row['receiver_id']} OR id = {$row['sender_id']}");
$username_stmt->execute();
$username_stmt->store_result();
$username_stmt->bind_result($id, $username);
$username_stmt->fetch();
$username_stmt->close();
$results->free();
return "
<div id='friend'>
<a href='profile.php?id=".$id."'><img src='". getProfileImagePath($id, $sqli_con) . "' /></a>
<a href='profile.php?id=".$id."'>". $username . "</a>
</div>
";
}
}
} else {
$results->free();
return "You don't have any friends yet! :( Why not search for some?";
}
}
目前没有返回任何结果,但如果我取出用户 ID 检查它只返回当前用户。
编辑:我得到它的工作,解决方案:
function getFriends($user_id, $sqli_con) {
$user_id = mysqli_escape_string($sqli_con, strip_tags($user_id));
$results = $sqli_con->query("SELECT * FROM friends WHERE (receiver_id = '$user_id' OR sender_id = '$user_id') AND accepted = '1'");
if($results->num_rows > 0) {
while($row = $results->fetch_array(MYSQLI_ASSOC)) {
if($row['sender_id'] !== $user_id) {
$username_stmt = $sqli_con->prepare("SELECT username FROM members WHERE id = {$row['sender_id']}");
$username_stmt->execute();
$username_stmt->store_result();
$username_stmt->bind_result($username);
$username_stmt->fetch();
$username_stmt->close();
echo "
<div id='friend'>
<a href='profile.php?id=".$row['sender_id']."'><img src='". getProfileImagePath($row['sender_id'], $sqli_con) . "' /></a>
<a href='profile.php?id=".$row['sender_id']."'>". $username . "</a>
</div>
";
}
if($row['receiver_id'] !== $user_id) {
$username_stmt = $sqli_con->prepare("SELECT username FROM members WHERE id = {$row['receiver_id']}");
$username_stmt->execute();
$username_stmt->store_result();
$username_stmt->bind_result($username);
$username_stmt->fetch();
$username_stmt->close();
echo "
<div id='friend'>
<a href='profile.php?id=".$row['receiver_id']."'><img src='". getProfileImagePath($row['receiver_id'], $sqli_con) . "' /></a>
<a href='profile.php?id=".$row['receiver_id']."'>". $username . "</a>
</div>
";
}
}
} else {
$results->free();
return "You don't have any friends yet! :( Why not search for some?";
}
}
不漂亮,但它会做。感谢您的帮助。(经过多个条目测试并且有效)