简体   繁体   中英

issues with simple database query

I have issues with this pretty simple database query. The opening if-condition is true and working.

The problem comes with the if/if/else inside. The second statement, when the active user has some connections, but the profile he is visiting does not, is working. Results in "invite to your crew".

But if the first state of nature is true, when neither the active user nor the profile owner has connections, my query isn't working. It will always result in the else statement ("join crew"), which should only happen when the profile owner has at least one connection ($countProfileCon!==0), in that case it won't matter how many connections the active user has.

I can't see why.

    if ($countCurrent==0 && $countFormer==0) {


        $activeConnectionQuery = $DBcon->query("SELECT * FROM tbl_current_userconnections WHERE user_id=".$_SESSION['userSession']);
        $countActiveCon=$activeConnectionQuery->num_rows;

        $profileConnectionQuery = $DBcon->query("SELECT * FROM tbl_current_userconnections WHERE user_id=".$_GET['user']);
        $countProfileCon=$profileConnectionQuery->num_rows;



        if ($countActiveCon==0 && $countProfileCon==0) {
            $connect = "<p>create a crew</p>";
        }

        if ($countActiveCon!==0 && $countProfileCon==0) {
            $connect = "<p>invite to your crew</p>";
        } 

        else {
            $connect = "<p>join crew</p>";
        }
    }

tbl_current_userconnections looks like this, when the problem occurs:

user_id                 connection_id
----------------        ----------------

(it's empty, obviously)

I think you are missing the "else" keyword in second condition.

if ($countActiveCon==0 && $countProfileCon==0) {
    $connect = "<p>create a crew</p>";
}

elseif ($countActiveCon!==0 && $countProfileCon==0) {
    $connect = "<p>invite to your crew</p>";
} 

else {
    $connect = "<p>join crew</p>";
}

Your first condition works fine. But because of missing elseif it overrides your variable, because second condition is evaluated as false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM