读取一个用户ID的许多记录

时间:2013-11-17 12:04:24

标签: php mysqli

我在两个表之间创建关系(login_pre,privilege) 和一个用户ID的许多访问级别 它们如下:

特权表:

+----------------------------------+
|  AccessLevel | login_id  | pre_id|
|----------------------------------|
|      1       |    1     |   1    |
|      2       |    1     |   2    | 
|      4       |    2     |   4    |
+----------------------------------+

这是login_pre table:

    +----------------------------------+
    |  username| userpass | login_id   |
    |----------------------------------|
    |      a   |    123   |   1        |
    |      a   |    123   |   1        | 
    |      b   |   1234   |   2        |
    +----------------------------------+

添加访问级别的管理员

如何创建为一个用户ID读取正确多条记录的php代码

我多次尝试此代码,但没有输出任何内容

   $query ="SELECT * 
FROM privilege AS privilege.login_id 
JOIN login_pre AS login_pre.login_id 
ON privilege.login_id=login_pre.login_id ";//output nothing

然后我尝试:

$query =
        "SELECT * 
        FROM privilege 
        where login_id='".$_SESSION['sessionloginid']."'  " ;

输出如下:

在特权页面中:

 if(isset($_SESSION['sessionloginid']))// point to id of user logged in
        {  
        $query =
        "SELECT * 
        FROM privilege 
        where login_id='".$_SESSION['sessionloginid']."'  " ;
$result = mysqli_query($link,$query) or die('');
        while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
                {


             $_SESSION['sessionloginid']= $row['login_id']; //output 11
           $_SESSION['sessionaccess']= $row['AccessLevel'];//output 12

                }
        }

并保护页面:

function protect_page()


{


    if($_SESSION['sessionloginid']== true && $_SESSION['sessionaccess'] !=1 )

    {
//header ('location:http://localhost/database/agtdatabase/agt_site/agtSite/agt2.php');
 //$_SESSION['sessionloginid']; // output 1  
 echo $_SESSION['sessionaccess'] ;//output 2 only for user loginid = 1 =>the problem
exit();             

    }
}
protect_page();

旁注我之前写了这个问题,然后按照答案但是没有输出,之后没有回复所以我写的问题更清楚我想要什么 为单个用户读取许多记录的代码

2 个答案:

答案 0 :(得分:0)

$query ="SELECT privilege1.AccessLevel 
FROM privilege AS privilege1 
JOIN login_pre AS login_pre1
ON privilege1.login_id=login_pre1.login_id ";

答案 1 :(得分:0)

您的查询有错误。看看这里:

$query ="SELECT * FROM privilege
JOIN login_pre
ON privilege.login_id=login_pre.login_id";