1,105,288 Community Members

Check if condition is true

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ?

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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...

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

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
diafol
Where are my eyes?
12,940 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
1
 

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.

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Question Answered as of 1 Year Ago by AndreRet and diafol
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article