douliu3831 2016-05-31 10:50
浏览 56
已采纳

一个函数里面的php会话变量

I am trying to have my function return both the username and the roleid. Here is the code that i have for this task:

session_start();
if($_SESSION['LoggedIn']){
$username = $_SESSION['username'];
function getUserRole($username, $roleid){
    $con = dbConnect();
    $query = "select * from user inner join userrole on user.id = userrole.userid inner join role on role.id = userrole.roleid where username = :username and roleid = :roleid";
    $sql = $con->prepare($query);
    $sql->bindValue(':username', $username);
    $sql->bindValue(':roleid', $roleid);
    $sql->execute();
    $row = $sql->fetch();
    $username = $row['username'];
    $roleid = $row['roleid'];

    if($row > 0){
        return  $username . $roleid;
    } else {
        return false;
    }
}

print getUserRole($username, $roleid);
}

I have three tables that look like this:

mysql> select * from role;
+---------------+----------------------------------+
| id            | description                      |
+---------------+----------------------------------+
| administrator | add, remove and edit manuscripts |
| reviewer      | review manuscripts               |
| site user     | read manuscripts                 |
+---------------+----------------------------------+
3 rows in set (0.00 sec)

mysql> select * from userrole;
+--------+---------------+
| userid | roleid        |
+--------+---------------+
|      1 | administrator |
|      2 | revinothingewer      |
|      3 | other         |
+--------+---------------+
3 rows in set (0.01 sec)

mysql> select * from user;
+----+----------+----------------------------------+
| id | username | password                         |
+----+----------+----------------------------------+
|  1 | kamau    | 80ce10e582e13ec085f13409c3add5a4 |
|  2 | admin    | db43b86da58631629adada27f1db5841 |
|  3 | amaina   | 7fc961ec5b6b5d51d2445e97775bfc0d |
+----+----------+----------------------------------+

The roleid value is derived from the following script:

$query = "select * from user inner join userrole on user.id = userrole.userid inner join role on role.id = userrole.roleid";
    $sql = $con->prepare($query);
    $sql->execute();
    $row = $sql->setFetchMode(PDO::FETCH_ASSOC);
    while ($row = $sql->fetch()){ 
    $roleid = $row ['roleid'];
    }

My question is why is var_dump (getUserRole($username,$roleid)) returning false?

  • 写回答

2条回答 默认 最新

  • dsxon40042 2016-05-31 13:07
    关注

    why is var_dump (getUserRole($username,$roleid)) returning false? Because

    if($row > 0){
    

    Every time this condition going to false part so only. If you want get username and roleid. You have to do following steps. 1. You know username.

    $username = $_SESSION['username'];
    

    2. You need to know roleid. So get roleid value by using following code.

    $con = dbConnect();
    $query = "SELECT roleid FROM user JOIN userrole ON user.id = userrole.userid WHERE user.username = :username";
    $sql = $con->prepare($query);
    $sql->bindValue(':username', $username, PDO::PARAM_STR);
    $sql->execute();
    $row = $sql->fetch();
    $roleid = $row['roleid'];
    

    Thats it. Now you know username value and roleid value. Keep it simple buddy. Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题