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?

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

Really Nice, thanks for sharing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.