0

我有一个从数据库中提取行的朋友页面,我只想显示当前用户的朋友,但它只从查询中返回当前用户。

数据库表:

在此处输入图像描述

当前代码:

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?";
    }
}

不漂亮,但它会做。感谢您的帮助。(经过多个条目测试并且有效)

4

2 回答 2

0

您从循环中返回。尝试类似:

$result_string = '';
// the loop definition here
$result_string .= "<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>";
// closing the loop
return $result_string;
于 2013-01-21T11:32:54.340 回答
0

不会$user_id总是出现在生成的记录集中吗?

而不是忽略记录,$user_id您需要从结果集中处理它们。提取记录中的其他ID,其他列给你一个朋友ID!

此外,实施 Lim 对记录集中多条记录的建议。

于 2013-01-21T11:41:13.027 回答