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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?