关系数据库问题

时间:2013-11-16 23:00:27

标签: php mysqli

我在两个表(login_pre,privilege)之间创建关系,它们如下:

特权表:

+----------------------------------+
|  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        |
    +----------------------------------+

添加访问级别的管理员

怎样才能使php代码读取access_vel = 1(1或2)的login_id = 1

我写了这个两页的代码,但是只读了accesslevel = 2的login_id = 1:

特权页面:

ob_start();
session_start();
include 'C:\xampp\htdocs\database\agtdatabase\agt_site\connection\connect.php';



$query ="SELECT * FROM privilege " ;
$result = mysqli_query($link,$query) or die('');    

             if(isset($_SESSION['sessionloginid']))// point to id of user logged in
        {  
        $query ="SELECT * FROM privilege AS privilege.login_id JOIN login_pre AS login_pre.login_id ON privilege.login_id=login_pre.login_id  WHERE login_id =1 "  ;

        $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 for user login id = 1

                }
        }

    ob_end_flush();

并保护页面:

<?php

include_once('C:\xampp\htdocs\database\agtdatabase\agt_site\login2\privilege.php');


function protect_page()
            {


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

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

    }
}
protect_page();
?>

1 个答案:

答案 0 :(得分:2)

单个查询怎么样:

SELECT * FROM privilege AS p JOIN login_pre AS l 
ON p.login_id = l.login_id 
WHERE AccessLevel IN (1, 2) AND l.login_id = 1;

更新

不确定您是否知道,但此部分始终会覆盖会话数据,因此您只能获取最后一行:

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
    $_SESSION['sessionloginid']= $row['login_id'];
    $_SESSION['sessionaccess']= $row['AccessLevel'];
}