Jump to content


Photo

*SOLVED* Automating fetching values from mysql table


  • Please log in to reply
6 replies to this topic

#1 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 01 August 2006 - 10:12 AM

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:

// 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;

so I image it would go:

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

If I havent been clear enough, please let me know and I will try and explain it better.....
If you're not part of the solution, you're part of the precipitate

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 August 2006 - 10:33 AM

Use a foreach loop:

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

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:
$_SESSION = mysql_fetch_array($r);


#3 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 01 August 2006 - 09:57 PM

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:

$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;
				}
			}


If you're not part of the solution, you're part of the precipitate

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 August 2006 - 10:19 AM

Use mysql_fetch_assoc as opposed to mysql_fetch_array. So use this:
$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;
        }
    }
}


#5 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 02 August 2006 - 08:37 PM

why
If you're not part of the solution, you're part of the precipitate

#6 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 08:59 PM

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.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#7 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 02 August 2006 - 10:17 PM

Sweet, thanks for the explaination  ;D
If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users