0

您好,我需要从名为“USERS”的表中选择所有具有特权 1、2 和 3 的用户。之后,我将使用他们的 ID 在另一个名为“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; ?>

更新 我无法获得“$login”变量的输出...如果我尝试输出“$name”或“$id”,它工作正常...但我需要输出“$login”。我想我可以在这里使用一些 INNER JOin ......

有什么线索吗?

谢谢。

解决方案

$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';
4

3 回答 3

1

您可以通过连接选择所有必填字段

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

然后删除您的第二个查询

于 2012-09-10T17:18:18.070 回答
0

基本上,Login 列似乎应该在 Status 表中,但它没有被使用。尝试这样的事情:

<?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; ?>
于 2012-09-10T16:52:08.517 回答
0

如果你只是得到 PHP 语法错误,你忘了关闭 while 循环。这是更正后的代码:

$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. :(';
    }
}

此外,您可能想改进您的产品线:

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

经过:

$result = mysqli_query($connect, $sql) or die ("database error: ".mysql_error().'<br />query: '.$sql);
于 2012-09-10T16:33:19.030 回答