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?