Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3279080
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:33:08+00:00 2026-05-17T19:33:08+00:00

I guess I have a little understanding about functions and how they work.. Idea

  • 0

I guess I have a little understanding about functions and how they work.. Idea is to get “user’s group” from database, but I don’t want to write the code evey time when I need it so I decided to put it in function.

function get_role()
{
 $user_id = $_SESSION['login_ok'];
 $res = mysql_query('SELECT role FROM users WHERE id = "'.$user_id.'"') or mysql_error();
 $row = mysql_fetch_array($res);
 $role = $row['role'];
}

and then when I need it

get_role();

What I need is $role variable which I will use in if() to grant access to smth. But when I do so, it shows that $role is Undefined variable (when I do that checking with if).

Sorry for probably dumb questions but what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:33:09+00:00Added an answer on May 17, 2026 at 7:33 pm

    Add return $role; to the end of your function.

    You then can do $role = get_role(); where you call the function.

    The reason your code is not working is called variable scope. The $role variable you are creating is only available within the function.

    By adding the code above, you return the value of $role from your function and assign it to a local variable where you called the function.

    You could also declare $role as global which will make it available everywhere, but this is considered a very bad practice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a little over my head with this but I guess you have
A little stuck here. I have a simple question I guess. Given the following
A fairly simple question for which I have a guess, but I can't find
I guess no one will have a definative answer to this but considered predictions
I guess this is kind of an odd question but I have tried setting
I guess it is impossible, but I will ask it anyway. I have a
I guess this is simple, but i couldnot figure it out. i have a
I have the following bean defined: <sec:authentication-manager alias=authenticationManager> <sec:authentication-provider user-service-ref=userDetailsService /> </sec:authentication-manager> I guess
To begin with, I know very little about DTSX files and SSIS, but my
I have a little bit of confusion about iOS Apps publication. The first time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.