PHP mysqli表查询给出了我想要查询另一个表的多个结果

时间:2013-03-12 15:38:32

标签: php mysqli

我一直在社交网络上工作,我有一个粉丝表,用户可以为自己喜欢的乐队或名人创建个人资料,还有另一个名为朋友的表,他们可以订阅这些粉丝。我希望请求显示在粉丝页面管理员的通知中除外。 在第一个sql查询之后,它返回具有多个值的fanpages但是如果我回显下一个$ sql查询,则表明它只选择一个结果来查询而不是全部查询。

所以基本上我需要查看所有通过登录用户创建的所有粉丝的节日表($ log_username),然后我需要获取这些粉丝并查询朋友表以查明是否有人请求订阅用户粉丝页面??

感谢你的帮助迈克尔

    <?php
$fanpage_requests = '';

$fansql = "SELECT created_by, fanpage_name FROM `fanpages`  WHERE created_by = '$log_username' ";
$fanquery = mysqli_query($db_conx, $fansql);
$fannumrows = mysqli_num_rows($fanquery);

if($fannumrows < 1){
    $fanpage_requests = 'No friend requests';
} else {

    while($row = mysqli_fetch_array($fanquery, MYSQLI_ASSOC)) {
        $fanpage_name = $row["fanpage_name"];
        $created_by = $row["created_by"];

        $fansubSql = "SELECT * FROM friends WHERE user2='$fanpage_name' AND accepted='0' ORDER BY datemade ASC";
        $fansubQuery = mysqli_query($db_conx, $fansubSql);
        $fansubNumrows = mysqli_num_rows($fansubQuery);
        //print_r ($fansubNumrows);     
        if($fansubNumrows < 1){
            $fanpage_requests = "blah blah"; 
        }
        print_r ($fansubNumrows);
        while ($fansubRow = mysqli_fetch_array($fansubQuery, MYSQLI_ASSOC)) {
            $fansubreqID = $fansubRow["id"];
            $fansubuser1 = $fansubRow["user1"];
            $fansubdatemade = $fansubRow["datemade"];
            $fansubdatemade = strftime("%B %d", strtotime($fansubdatemade));

            $fansubthumbquery = mysqli_query($db_conx, "SELECT avatar FROM users WHERE username='$fansubuser1' LIMIT 1");
            $fansubthumbrow = mysqli_fetch_row($fansubthumbquery);
            $fansubuser1avatar = $fansubthumbrow[0];
            $fansubuser1pic = '<img src="user/'.$fansubuser1.'/'.$fansubuser1avatar.'" alt="'.$fansubuser1.'" class="user_pic">';

            if($fansubuser1avatar == NULL){
                $fansubuser1pic = '<img src="images/avatardefault.jpg" alt="'.$fansubuser1.'" class="user_pic">';
            }

            $fanpage_requests .= '<div id="friendreq_'.$fansubreqID.'" class="friendrequests">';
            $fanpage_requests .= '<a href="user.php?u='.$fansubuser1.'">'.$fansubuser1pic.'</a>';
            $fanpage_requests .= '<div class="user_info" id="user_info_'.$fansubreqID.'">'.$fansubdatemade.' <a href="user.php?u='.$fansubuser1.'">'.$fansubuser1.'</a> requests friendship<br /><br />';
            $fanpage_requests .= '<button onclick="fanReqHandler(\'accept\',\''.$fansubreqID.'\',\''.$fansubuser1.'\',\'user_info_'.$fansubreqID.'\')">accept</button> or ';
            $fanpage_requests .= '<button onclick="fanReqHandler(\'reject\',\''.$fansubreqID.'\',\''.$fansubuser1.'\',\'user_info_'.$fansubreqID.'\')">reject</button>';
            $fanpage_requests .= '</div>';
            $fanpage_requests .= '</div>';
        }
    }
}
?>

print_r输出现在是001110而且我得到了等等。

1 个答案:

答案 0 :(得分:0)

好的,所以确定你需要稍后关闭你的循环,它无法正常工作的原因是因为现在你在不同的循环中使用相同的变量,所以值被覆盖(例如$query )。

您需要根据需要变化的所有变量清楚地区分您的循环:

<?php
    $friend_requests = '';

    $sql = "SELECT created_by, fanpage_name FROM `fanpages`  WHERE created_by = '$log_username' ";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);

    if($numrows < 1){
        $friend_requests = 'No friend requests';
    } else {

        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
            $fanpage_name = $row["fanpage_name"];
            $created_by = $row["created_by"];

            $friendsSql = "SELECT * FROM friends WHERE user2='$fanpage_name' AND accepted='0' ORDER BY datemade ASC";
            $friendsQuery = mysqli_query($db_conx, $friendsSql);
            $friendsNumrows = mysqli_num_rows($friendsQuery);
            $fanpage_requests = "$friendsSql";

            if($friendsNumrows < 1){
                $fanpage_requests = "$fanpage_name"; **//this shows that only 1 query is being queried in this second sql statement**
            }

            while ($friendsRow = mysqli_fetch_array($friendsQuery, MYSQLI_ASSOC)) {
                $reqID = $friendsRow["id"];
                $user1 = $friendsRow["user1"];
                $datemade = $friendsRow["datemade"];
                $datemade = strftime("%B %d", strtotime($datemade));

                $thumbquery = mysqli_query($db_conx, "SELECT avatar FROM users WHERE username='$user1' LIMIT 1");
                $thumbrow = mysqli_fetch_row($thumbquery);
                $user1avatar = $thumbrow[0];
                $user1pic = '<img src="user/'.$user1.'/'.$user1avatar.'" alt="'.$user1.'" class="user_pic">';

                if($user1avatar == NULL){
                    $user1pic = '<img src="images/avatardefault.jpg" alt="'.$user1.'" class="user_pic">';
                }

                $fanpage_requests .= '<div id="friendreq_'.$reqID.'" class="friendrequests">';
                $fanpage_requests .= '<a href="user.php?u='.$user1.'">'.$user1pic.'</a>';
                $fanpage_requests .= '<div class="user_info" id="user_info_'.$reqID.'">'.$datemade.' <a href="user.php?u='.$user1.'">'.$user1.'</a> requests friendship<br /><br />';
                $fanpage_requests .= '<button onclick="fanReqHandler(\'accept\',\''.$reqID.'\',\''.$user1.'\',\'user_info_'.$reqID.'\')">accept</button> or ';
                $fanpage_requests .= '<button onclick="fanReqHandler(\'reject\',\''.$reqID.'\',\''.$user1.'\',\'user_info_'.$reqID.'\')">reject</button>';
                $fanpage_requests .= '</div>';
                $fanpage_requests .= '</div>';
            }
        }
    }
?>