Jump to content

Storing and accessing CONSTANTS in a database


maexus

Recommended Posts

This used to work before but when I rewrote it, it doesn't..

My userlevels are stored as constants

[code]
//User Levels
define('ADMIN',        4);
define('MOD',        3);
define('USER',        2);
define('GUEST',        1);
define('BANNED',    0);
[/code]

My registration script stores these userlevel titles as a string to access later on during login and page security. Before I could say..

[code]
$results = mysql_query('some query');
$row = mysql_fetch_assoc($results);

echo $row['userlevel'];
[/code]

That should output like USER which is a constant so it really outputs 2 into the script. That's what it used to do but now it outputs USER, which is no good. I have to end up using

[code]
$user['level'] = constant($row['userlevel']);
[/code]

Now this works but it's annoying because I never had to do this before... My question is, what is the best way to store a constant name in a DB then spit the value back out, not as a string, but as something the PHP parsing will automaticly parse as a constant?

Sorry if this is confusing.
Link to comment
Share on other sites

Guest footballkid4
If you really wanted to, you could store:
define( "MOD" , 3 );
In the DB, and then run eval() when you pull things from the DB...but that's not the best way to do it.

You can store the information like this: MOD|3

Then your script would look similar to:
[code]<?php
$query = "queryhere";
$result = mysql_fetch_array( $query ) or die( mysql_error() );
while ( $row = mysql_fetch_array( $result ) )
{
  $levels = explode( "|" , $row['userlevel'] );
  define( $levels[0] , $levels[1] );
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.