Hello guys,
have a nice day...
I've created function to test if the user is admin or not and return true or false depending on user

function isitadmin($userid) {
$query = mysql_query("SELECT * FROM users WHERE user_id = '". $userid ."' AND level = 1"); 
    if (mysql_num_rows($query) > 0) 
    { 
        return true; 
    }
    else
    {
        return false; 
    }

 }

if I test this function seperatly in single page the output will be "1" if admin and "nothing" if regular user
but when I use it in php file , like code below:

<?php
if (isitadmin($user_id)) {// $user_id comes from DB
// show some admin activities
 } else {
// show normal user activites
} 

but it shows admin activities for everyone :(, whats wrong then ?

Recommended Answers

All 5 Replies

I don't see why you are using an if statement again, it was already called in the function. What basically happens in your if statement is that you check if the function was called, which it was, hence all admin returns. Just call the function..

<?php
    isitadmin($user_id)
?>

Rather put the code what you want to do with an admin in your function where it returns 1 or more rows...

I need isitadmin to return value that confirm this user is admin so it will show some buttons or can access to diffrent areas so I must put true of false as return,
I've tried to call function and tried to put "1" as return and now nothing appears to admin or user

Change your function to -

function isitadmin($userid) {
$query = mysql_query("SELECT * FROM users WHERE user_id = '". $userid ."' AND level = 1"); 
    if (mysql_num_rows($query) > 0) 
    { 
        echo '<input type="text" name="admin"'; 
        //This will show the control (and any others you want for admin)
        //Add more admin controls here...
    }
    else
    {
        echo 'You are not an admin, no buttons here for you'; 
        //You can add whatever controls you want here if NOT an admin...
    }
 }

Now call the function where you want to call it...

<?php
    isitadmin($user_id)
?>
Member Avatar for diafol

I think you need the conditional in order to evaluate the result from the function, don't you?

function isitadmin($userid) {
$str = "SELECT * FROM users WHERE user_id = '". $userid ."' AND level = 1";
echo $str;
exit();
$query = mysql_query($str); 
    if (mysql_num_rows($query) > 0) 
    { 
        return true; 
    }
    else
    {
        return false; 
    }
 }

Echo the SQL string to the screen. Highlight and copy it to the clipboard and paste it to the SQL window in phpmyadmin. Run it, see what it gives.

commented: thank for this advice +1

I was having problem with $user_id so always its an error , because the $user_id was taking from files uploader not from session so after I edited the $user_id source it solved my problem, thanks for @diafol for this advice by echo the $sql. SOLVED.

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.