简体   繁体   中英

MySQL - Select Users with certain privileges (INNER JOIN)

Hello I need to select all users with privileges, 1, 2 and 3 from a table named "USERS". After that, I will use their ID to do a new select at another table, named "Status".

<?php
include_once("include/connection.php");
$sql = 'select * from USERS where Privileges != "4"';

$rs = mysqli_query($connect, $sql) or die ("error");
$op = mysqli_num_rows($rs);

while($row = mysqli_fetch_array($rs))
{
    $id = $row['ID'];
    $name = $row['Name'];
    $priv = $row['Privileges'];

    $sql = 'select * from Status where ID="$id"';
    $result = mysqli_query($connect, $sql) or die ("database error");
    $gmon = mysqli_num_rows($result);

    $login = $row['Login'];

    switch($login)
    {
        case "ONLINE":
            $login = "<font color=\"#84FB84\"><strong>".$name."</strong></font>";
            break;
        case "OFFLINE":
            $login = "<font color=\"#46AAEB\"><strong>".$name."</strong></font>";
            break;
    }
    $online_adms .= "<td>$login</td>";
}
?>
<?php echo $online_adms; ?>

UPDATE I can't get the output for "$login" variable... If I try to output "$name" or "$id", it works fine...but I need to output the "$login". I guess I can use some INNER JOin here...

Any clues?

Thanks.

SOLUTION

$sql = 'SELECT c.Name, c.ID, c.Privileges, d.Login FROM USERS c 
        INNER JOIN Status d 
        ON c.ID = d.ID
        where Privileges != 4';

you could select all the required fields with a join

SELECT `ID`,`Name`,`Privileges`,`Login` 
 FROM `USERS` 
 INNER JOIN `Status`
 ON `USERS`.`ID` = `Status`.`ID`
 WHERE Privileges != 4

then remove your second query

If you are just getting PHP syntax errors, you forgot to close the while loop. Here is the corrected code:

$sql = 'select * from USERS where Privileges != "4"';

$rs = mysqli_query($connect, $sql) or die ("error");
$op = mysqli_num_rows($rs);

while($row = mysqli_fetch_array($rs))
{
    $id = $row['ID'];
    $name = $row['Name'];
    $priv = $row['Privileges'];

    $sql = 'select * from Status where ID="$id"';
    $result = mysqli_query($connect, $sql) or die ("database error");
    $gmon = mysqli_num_rows($result);

    $login = $row['Login'];

    switch($login)
    {
        case "ONLINE":
            $login = "<font color=\"#84FB84\"><strong>".$name."</strong></font>";
            break;
        case "OFFLINE":
            $login = "<font color=\"#46AAEB\"><strong>".$name."</strong></font>";
            break;
        default:
            $login = 'OOPS! The value of $login is '.$login.' that is why nothing is being printed. :(';
    }
}

Also, you might want to improve your line:

$result = mysqli_query($connect, $sql) or die ("database error");

by:

$result = mysqli_query($connect, $sql) or die ("database error: ".mysql_error().'<br />query: '.$sql);

Basically, it appears that the Login column is supposed to be in the Status table, but it wasn't being used. Try something like this:

<?php
include_once("include/connection.php");
$sql  = 'SELECT USERS.*, STATUS.* FROM USERS INNER JOIN STATUS ';
$sql .= 'ON USERS.ID=STATUS.ID WHERE USERS.Privileges != "4"';

$rs = mysqli_query($connect, $sql) or die ("error");
$op = mysqli_num_rows($rs);

while($row = mysqli_fetch_array($rs))
{
    $id = $row['ID'];
    $name = $row['Name'];
    $priv = $row['Privileges'];
    $login = "<font color=\"#46AAEB\"><strong>".$name."</strong></font>";
    $loginStatus = $row['Login'];

    switch($loginStatus)
    {
        case "ONLINE":
            $login = "<font color=\"#84FB84\"><strong>".$name."</strong></font>";
            break;
        case "OFFLINE":
            $login = "<font color=\"#46AAEB\"><strong>".$name."</strong></font>";
            break;
        default:
            $login = "<font color=\"#999999\"><strong>".$name."</strong></font>";
            break;
    }
    $online_adms .= "<td>$login</td>";
}
?>
<?php echo $online_adms; ?>

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