Jump to content

Archived

This topic is now archived and is closed to further replies.

sanfly

*SOLVED* Automating fetching values from mysql table

Recommended Posts

Lets say I have a table in my database that stores "permission" values for my content management system.

When the user logs in, I have a script that extracts these permissions and stores them as sessions to be used around the CMS as needed.

I currently have a script that manually sets all these permissions (see code snippet below), but Im getting sick of manually updating and adding the code for new permissions every time I add a new module or function. 

What I would like to do is have a script that can automatically detect the permissions and set the sessions.  The permission values are either 0 (not allowed) or 1 (allowed).

The script I currently have is like this:

[code]// Permissions
include "db.php";
$q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_groups'";
$r = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($r);
// Admin Panel
$perm_adminpanel = $row['perm_adminpanel']; // Access to admin panel
$_SESSION['perm_adminpanel'] = $perm_adminpanel;
$perm_configadmin = $row['perm_configadmin']; // Configure Admin Settings
$_SESSION['perm_configadmin'] = $perm_configadmin;
$perm_setperm = $row['perm_setperm']; // Allowed to set permissions for groups
$_SESSION['perm_setperm'] = $perm_setperm;
$perm_addgroup = $row['perm_addgroup']; // Allowed to create new groups
$_SESSION['perm_addgroup'] = $perm_addgroup; [/code]

so I image it would go:

[code]
include "db.php";
$q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_groups'";
$r = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($r);

// here would invoke some kind of loop?
$value = $row[$name];
$_SESSION['name'] = $value;
// end the loop
[/code]

If I havent been clear enough, please let me know and I will try and explain it better.....

Share this post


Link to post
Share on other sites
Use a foreach loop:

[code=php:0]// loop through the $row array, setting up the sessions vars automatically.
foreach($row as $key => $value)
{
    $_SESSION[$key] = $value;
}[/code]

Make sure you have session_start(); as the first line in permission file, in order for the session variables to populate.

EDIT: Come to think of it you might not need a foreach loop, instead your can just do this:
[code=php:0]$_SESSION = mysql_fetch_array($r);[/code]

Share this post


Link to post
Share on other sites
I had tried that foreach array, but for some reason each one came up twice, once with a numeric key, and once with the name of the key.

However, since all my permissions start with "perm_", I ended up using the following to only set the named keys (not the numeric keys) as sessions:

[code=php:0] $q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_group'";
$r = mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($r);

foreach( $row AS $key => $val ){
if(strstr($key, "perm_")){
//echo "$key, $val<br>";
$_SESSION[$key] = $val;
}
}

[/code]

Share this post


Link to post
Share on other sites
Use mysql_fetch_assoc as opposed to mysql_fetch_array. So use this:
[code=php:0]$q = "SELECT * FROM u_groups WHERE ugroup_id = '$user_group'";
$r = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_assoc($r)
{
    foreach( $row AS $key => $val )
    {
        if(strstr($key, "perm_"))
        {
            //echo "$key, $val<br>"
            $_SESSION[$key] = $val;
        }
    }
}[/code]

Share this post


Link to post
Share on other sites
mysql_fetch_array fetches BOTH the associative and numeric keys, so you get the columns twice. however, mysql_fetch_assoc only fetches the associative keys, so it's better in this case. you can also get rid of that strstr thing since you no longer have the numeric keys.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.