0

I am trying to use a function to return a simple value from a database, while this is usually easy, I am having some difficulty. I have tried several different mixtures with no success so I have decided to turn to this community for help.

The concept is a database structure has ranks with pagetypes that are assigned permissions.

Current columns for this example: rank / modreports / plreports

Data Example: admin / 1 / 1

function CheckPermission($module){
    $GetPerms = mysql_query("SELECT * FROM `permissions` WHERE `rank` = '".$MyDetails['group']."' LIMIT 1");
    $MyPerm = mysql_fetch_array($GetPerms);
        if($MyPerm[$module] != 1){
            echo 'DEBUG: '.$MyPerm[$module];
            //redirect("index.php?resp=perms");
            die();
        }
}

Usage Example:

CheckPermissions(modreports);

The above usage should return DEBUG: 1 at this stage OR show nothing at all (preferably the second one), instead it shows DEBUG:. $MyPerm['modreports'] will return the correct value so I assume it to be the variable within the variable which I have only used in $_POST[''] in the past.

Thanks for your help, I hope I have provided enough information to get some help.


In response to the assistance, this is the corrected code;

function CheckPermission($module,$group){
    $GetPerms = mysql_query("SELECT * FROM `permissions` WHERE `rank` = '".$group."' LIMIT 1");
    $MyPerm = mysql_fetch_array($GetPerms);
        if($MyPerm[$module] != 1){
            redirect("index.php?resp=perms");
            die();
        }
}
2
  • 1
    You need to add some basic error handle on your mysql query operations to this code. That would illuminate your problem immediately. I would also highly suggest writing you query to a variable and var_dumping that variable. again your problem will become obvious. Finally, if you had error reporting turned on with all levels, you would get a notice here that would also point you to the problem. So in summary, your biggest issue is that you need to really focus on learning how handle to debug your code, handle edge/error cases, and always keep an eye on your error messaging when developing. Commented Feb 22, 2014 at 1:14
  • @MikeBrant Thank you, In other situations I generally do, which is why the last time this issue occured I self-solved it. Complete overlook on my behalf this time. Commented Feb 22, 2014 at 1:22

3 Answers 3

1

Your function is not aware of $MyDetails or $MyPerm. It is only aware of the variables you pass into it (as you have done with $module), or those that you declare to be global (not recommended). You mention using $_POST in the past, this is a "superglobal". These are special cases and are available to all functions without further definitions.

Recommended reading: http://www.php.net/manual/en/language.variables.scope.php

Sign up to request clarification or add additional context in comments.

1 Comment

This makes perfect sense. I have come across the issue in the past and usually correct it myself. Simply passing the group to the function worked perfectly first time. MyPerms and the variable within it both worked perfectly as is aswell. =) Thank you.
0

I think, there is a wrong "not" in your condition:

Instead of

if($MyPerm[$module] != 1){

it should be

if($MyPerm[$module] == 1){

1 Comment

I reworded my apparntly poor choice of words, completly my fault. The problem is that it is returning blank, not the actual database value.
0

Given this table as the example data:

rank | modreports | plreports
------------------------------
admin|     1      |     1

The ($MyPerm[$module] != 1) will result False since 'modreports' has a value of 1. Therefore the 'echo 'DEBUG: '.$MyPerm[$module];' line will never be executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.