将每个php行值赋予php变量

时间:2018-06-01 06:30:16

标签: php sql sql-server

如何将每行的值转换为php变量/ session。对不起我的英语不好。我登录的过程是
1.输入用户名和密码
2.查询

SELECT * FROM tblUsers 
LEFT OUTER JOIN tblUserSubRight ON tblUserSubRight.ContactID = tblUsers.ContactID
WHERE tblUsers.UserID='$user' AND tblUsers.UsrPassword='$encr
  1. 检查用户是否可以访问tblUserSubRight
  2. 接入开关放入会话变量
  3. 结果表:

    |UserID|UsrPassword|SubMenuID             |Switch|ContactID|
    ____________________________________________________________
    |RSG   |ASDASSFSF  |Coordinator Activities|1     |1        |
    |RSG   |ASDASSFSF  |Activities Gallery    |1     |1        |
    |RSG   |ASDASSFSF  |Retailer Sales        |1     |1        |
    

    注意:
    UserID 是用户名
    ContactID 是用户的身份
    SubMenuID 是用户具有的不同访问权限 如果" 1" 开关为1或0用户有权访问,反之亦然

    我想让每一行将各自的开关值放入变量中。

    这是我的代码:

    $sql = "SELECT * FROM tblUsers LEFT OUTER JOIN tblUserSubRight 
            ON tblUserSubRight.ContactID = tblUsers.ContactID
            WHERE tblUsers.UserID='$user' AND tblUsers.UsrPassword='$encr'";
    
        $result = sqlsrv_query($conn, $sql, array(), array( "Scrollable" => 'static' ));
    
        $count = sqlsrv_num_rows($result);
    
        if($count > 0){
            date_default_timezone_set('Asia/Manila');
    
            $sql = "INSERT INTO tblUserLogs (ContactID, LogType, LogDate, DatabaseName) VALUES ('".$row["ContactID"]."', 'Login', '".date('Y-m-d H:i:s')."', '".$dbname."')";
            sqlsrv_query($conn, $sql);
    
            $_SESSION['id'] = $row['ContactID'];
            $_SESSION['user'] = $row['UserID'];
    
            if($row["UserLevel"] == "1"){
                $_SESSION['svisor'] = "";
            }
            else {
                $_SESSION['svisor'] = $row['ContactID'];
            }
    
            $_SESSION['host'] = $host;
            $_SESSION['dbname'] = $dbname;
            $_SESSION['dbserver'] = $db_conn;
    
            while($row = sqlsrv_fetch_array($result)){
                if($row["SubMenuID"] == "Coordinator Activities" && $row["Switch"] == "1") {
                    $_SESSION["actaccess"] = "1";
                }
                else {
                    $_SESSION["actaccess"] = "0";
                }
    
                if($row["SubMenuID"] == "Retailer Sales" && $row["Switch"] == "1") {
                    $_SESSION["salesaccess"] = "1";
                }
                else {
                    $_SESSION["salesaccess"] = "0";
                }
    
                if($row["SubMenuID"] == "Retailer Details" && $row["Switch"] == "1") {
                    $_SESSION["retaileraccess"]  = "1";
                }
                else {
                    $_SESSION["retaileraccess"] = "0";
                }
    
                if($row["SubMenuID"] == "Distance Matrix" && $row["Switch"] == "1") {
                    $_SESSION["distanceaccess"]  = "1";
                }
                else {
                    $_SESSION["distanceaccess"] = "0";
                }
    
                if($row["SubMenuID"] == "Activities Gallery" && $row["Switch"] == "1") {
                    $_SESSION["galleryaccess"]  = "1";
                }
                else {
                    $_SESSION["galleryaccess"] = "0";
                }
    
                if($row["SubMenuID"] == "Admin Tools" && $row["Switch"] == "1") {
                    $_SESSION["toolsaccess"]  = "1";
                }
                else {
                    $_SESSION["toolsaccess"] = "0";
                }   
            }
    
            $_SESSION['logged_in'] = true;
    
            header("location: dashboard.php");
        }
        else {
            echo 'error';
        }
    

0 个答案:

没有答案