0

I know this is very basic but i am not able to get the fuction return a string. Instead it is returning the value 1. Below is the code:

<?php
function getUserRole($username, $roleid){
include "data.php";
$con=dbConnect();
$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->bindValue(':username',$username);
    $sql->bindValue(':roleid',$roleid);
    $sql->execute();
    $row = $sql->fetch();

        if($row > 0){

            return true;
        }
        else{
            return false;
        }

}

print getUserRole('admin',$roleid);
?>

The tables look as follows

mysql> select * from user;
+----+----------+----------------------------------+
| id | username | password                         |
+----+----------+----------------------------------+
|  1 | kamau    | 80ce10e582e13ec085f13409c3add5a4 |
|  2 | admin    | db43b86da58631629adada27f1db5841 |
+----+----------+----------------------------------+
2 rows in set (0.00 sec)

mysql> select * from role;
+-----------+----------------------------------+
| id        | description                      |
+-----------+----------------------------------+
| admin     | 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 | reviewer      |
|      3 | other         |
+--------+---------------+
3 rows in set (0.00 sec)

What can i be possibly doing wrong?

2
Contributors
2
Replies
25
Views
1 Year
Discussion Span
Last Post by elisajames
0

It seems like sometimes the brain refuses to function...Mine had refused to work but after a small break from my desktop i "found"the solution.See below

function getUserRole($username, $roleid){
include "data.php";
$con=dbConnect();
$query="select * from user inner join userrole on user.id = userrole.userid inner join role on role.id = userrole.roleid where userid = 2 ";
$sql=$con->prepare($query);
    $sql->bindValue(':username',$username);
    $sql->bindValue(':roleid',$roleid);
    $sql->execute();
    $row = $sql->fetch();   
            if($row > 0){
                $username = $row['username'];
                $roleid = $row['roleid'];   
                return  $roleid . $username;
            }
            else{
                return false;
            }

}
print getUserRole($username,$roleid);

Edited by Amaina

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.