0
function find_user_roles($userid){
    $statement = pdo->prepare("SELECT users_roles.roles_id, roles.user_role FROM users_roles JOIN roles ON users_roles.roles_id = roles.id WHERE users_roles.users_id = $userid");
    $statement->execute();
    $result = $statement->fetchAll();
    print_r($result); //Returns the full Array
    return $result;
}
print_r($result); //brings Error: Uncaught ErrorException: Undefined variable: result

I dont understand why nothing returns from this function. Can you pls Help me. thx

daencore
  • 9
  • 2
  • 3
    You need to set `$result = find_user_roles(1)` (or whatever id), before calling `print_r`. – Reed Mar 11 '21 at 22:19
  • 2
    Does this answer your question? [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – El_Vanja Mar 11 '21 at 22:20
  • The code you've shared **defines** `find_user_roles`, but does not **call** it. It looks like you're expecting `$result` to be accessible outside `find_user_roles`, but `$result` only exists inside the `find_user_roles` function, as currently written. – Reed Mar 11 '21 at 22:22
  • Also, welcome to SO! Glad to have you hear, looking for help. :) – Reed Mar 11 '21 at 22:23
  • 1
    @Reed Thank You that was the solution. Now it Works perfectly. – daencore Mar 11 '21 at 22:37

0 Answers0