leoarce
leoarce

Reputation: 549

how do i check for mysql database table field value in creating a query?

if($_SESSION['usergroup']==1) { //admin
    $result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

else if($_SESSION['userid']==1013) { //managers
    $result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

else { //everyone else
    $result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

so what i would like to do is to change this line here:

else if($_SESSION['userid']==1013) {

i want to check if the logged in user has a value of 1 in the user_table in a field called manager. the pseudo code would be something like:

else if(user_table manager field == 1) {

or

else if(if logged in user has a value of 1 in the manager field of the user_table table) {

does this sound like something that can be done?

what i'm trying to accomplish is to edit users and make certain users managers, but i don't want to have to keep editing php files to keep adding those new users every time i make a user a manager. i just want the users that have been upgraded to have access to that middle query automatically.

here is what i don't want to do...

else if($_SESSION['userid']==1013 || $_SESSION['userid']==1014 || $_SESSION['userid']==1015 || $_SESSION['userid']==1016) {

...and keep adding and adding to this line in this fashion.

Upvotes: 1

Views: 836

Answers (1)

BoydDensmore
BoydDensmore

Reputation: 181

That definitely sounds like something that can be done. I would use something like this, using PDO to prepare and then execute the statement.

//Prepare the SQL query, using the :user_id parameter which you'll supply in the next statement
$stmt = $con->prepare('SELECT manager FROM user_table WHERE userid = :user_id');
//Execute the SQL, supplying the parameter
$stmt->execute(array(':user_id' => $_SESSION['userid'])'
//Retrieve the value
$manager_role = $stmt->fetchColumn();

Or, you can do the same thing without using PDO by preparing your SQL query before running it.

$sql_query = 'SELECT manager FROM user_table WHERE userid = ' . $_SESSION['userid'];
$manager_role =  = mysql_query($sql_query);
....
//Your original code
if($_SESSION['usergroup']==1) { //admin
$result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

else if($manager_role == 1) { //managers
    $result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

else { //everyone else
    $result = mysql_query("the select statement which is written a certain way. this part is not what question is about") or die(mysql_error());
}

....

Upvotes: 1

Related Questions