Help - Search - Members - Calendar
Full Version: If (mysql Database Variable == 1) Do Something?
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Sir Realism
Ok well just recently I added the login script from the tutorial section and got it to work like a charm, however I edited it and added another section which can only be seen in the phpmyadmin section, to only select few members which i set to "admin".
I also got that set up.
but now I'm planning on checking whether admin == 1 then something happens, any ideas?

Thanks for your time.
Jacob
I am not 100% sure what you are meaning here? Could you please post a little bit of source or a snippet so I can see what you mean?
Sir Realism
Well I have a table called users in a database and one of the rows is "admin" and it is either set to 1 or 0. if 1, then that user is an admin.
but somehow, I need a way to check whether that user is an admin or not? So I was thinking using an if statement for example

If ($admin == 1) }
$allow == 1
{
Else
}
$allow == 0
}
NDBoost
Ill write you up a quick PHP statement that pulls the table data and checks.

Ideally you should have two tables configured.

a users table which stores all of the users information, passwords, ID, username, group ID, contact details etc...
a groups table which stores the group name, its permissions and its group ID

You relate the two by pulling the column group_id from the users table and checking it against the groups table to see that it matches the admin group ID. If it does, grab the permissions available, and continue.. ill leave the last part up to you because depending how complex you need this to be..

There are tons of other ways to do this, this is just ONE way to do it.

I also threw this code together in a matter of 5 minutes so it is not tested to make sure its working.. Also make sure to sanitize the $uid var when you receive it if its coming from $_POST or $_GET.

CODE
<?php
#store db credentials in array $db[x]
$db = array(  
            "database" => "database1",  
            "username" => "dbUser",  
            "password" => "pass",  
            "host" => "localhost"  
         );
mysql_connect($db['host'],$db['username',$db['password']);
@mysql_select_db($db['database']) or die( "Unable to select database");

#Query users table and retrive user's group data based on provided $uid
#$uid is previously set above from your login script.
$query = "SELECT `gid` FROM `users` WHERE `uid`='$uid'"; #set our query to a variable
$result = mysql_query($query) or die(mysql_error());  #run the query, set results to $result
mysql_fetch_assoc($result); #put the query results into an array
$gid = $result['gid']; #assign group ID to a variable for easier handling

#next lets connect to the user_groups table and fetch the permissions available to us.
$query = "SELECT `gid`,`perms` FROM `user_groups` WHERE `gid`='$gid'";
$result = mysql_query($query) or die(mysql_error());
mysql_fetch_assoc($result);

################################################################################

################################################################################

################################################################################

#############PERMISSIONS CONDITIONAL STATEMENTS WILL GO HERE####################
################################################################################

################################################################################

################################################################################


mysql_close();
?>
NDBoost
I forgot to mention I would use a mysql wrapper of some sorts to cut down on the amount of mysql_query and fetch_assoc's youre running. Google search for mysql wrappers and youll know what im talking about.

its a much cleaner approach.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.