Jump to content

[SOLVED] storing 1 value configurations? (best method?)


Michdd

Recommended Posts

What would be the best method to store 1-value configuration details? I'm not talking about configurations that would just be written to a file, because they would be changeable. Previously I've created a table named configuration with the rows configname and configvalue. But this is really inefficient because I'd have to run a separate query for each configuration option (Assuming I'm retrieving more than one). How do you guys handle this type of thing?

personally I create a configuration file that defines a number of constants.

 

constants are addressed more efficiently than variables, they exist in the global scope and cannot be changed at runtime.

 

OR you could use parse_ini_file and create your custom configuration in there - the scope of the array returned is not global or so I believe.

this is really inefficient because I'd have to run a separate query for each configuration option (Assuming I'm retrieving more than one). How do you guys handle this type of thing?

 

Why execute separate queries?

 

$sql = "SELECT configname, configvalue FROM configs";
if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        $config = array();
        while ($row = mysql_fetch_assoc($result)) {
            $config[$row['configname']] = $row['configvalue'];
        }
    }
}

 

You now have an array $config that contains all your configuration directives.

Yea, I guess that's a good solution.

 

What do you think's a better (More professional) way to do it? Writing a file that defines all configuration values as constants, or your idea (Can't believe I never even though of that, lol).

well... your reading the database everytime your load the page.... you should store it in a session

That's a horrible idea..

 

I can't really decide which way to choose from. I'll probably go with rewriting a file which defines configuration options.

 

Thanks.

well... your reading the database everytime your load the page.... you should store it in a session

That's a horrible idea..

 

I can't really decide which way to choose from. I'll probably go with rewriting a file which defines configuration options.

 

Thanks.

 

please explain why storing it in a session would be a horrible idea? rather than querying the database everytime you need the variables, why not do it once and store it in a session?

 

$sql = "SELECT configname, configvalue FROM configs";
if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
        $config = array();
        while ($row = mysql_fetch_assoc($result)) {
            $_SESSION[config][$row['configname']] = $row['configvalue'];
        }
    }
}

Two reasons:

 

 

-If you're going to cache it, why not cache it for all users instead of a per user basis?  Like writing a file to disk or something.

-What if the config changes?

 

-then you wouldnt use the database to store them

-generally when I code, my config files dont change often at all... they are generally static but my need to change from installation to installation

 

although i dont use a database to store config vars, i use an include file... but if you did storing it in a session would be the best way

Storing it in a session really has no point.. If the configuration changes and it's something urgent it won't take effect right away (Unless you're doing a check to make sure it's up-to-date, which would completely null any point on doing this). It's not like loading the configurations every time a page loads puts much of a strain on anything, nor is it a bad practice.

 

Over all I think I'm going to go with rewriting a PHP file to include new constant definition each time a configuration is updated just because it seems like a cleaner method to me.

It's not like loading the configurations every time a page loads puts much of a strain on anything, nor is it a bad practice.

 

I guess your not used to dealing with high traffic sites

Even in high traffic websites a single additional query isn't going to cause much of an issue..

Two reasons:

 

 

-If you're going to cache it, why not cache it for all users instead of a per user basis?  Like writing a file to disk or something.

-What if the config changes?

 

-then you wouldnt use the database to store them

-generally when I code, my config files dont change often at all... they are generally static but my need to change from installation to installation

 

although i dont use a database to store config vars, i use an include file... but if you did storing it in a session would be the best way

 

-That doesn't make sense.  If you're going to cache it for person A using file x, why not also cache it for person B using file x?

 

 

 

 

Michdd, unless you plan on making a script that can easily alter config stuff (in other words, you want the end user to not have to open a .php file or alter a DB table or something), I would just go with flat file stuff...  Really the DB offers no advantage.

 

You still have the same problems:

-Variable scope

-Useless reloading of data

-Variable collisions

-So on....

 

Sometimes I wrap my config stuff in an object (I made a pretty simple class a while back that can load config from XML or ini files...  I meant to also make it able to do databases, but I never really got around to it...) to partially alleviate the variable scope issue, but then that gets messy too.

 

 

Pretty much I've just concluded that there isn't a perfect way to load configuration.

 

 

REMEMBER - your configuration settings will more likely need to available in the global scope.

 

For this reason I include a file with a series of define() statements.  If you must create an array then you will need to ensure it is available in every function or class - defining constants automatically makes these available globally.

Archived

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

×
×
  • 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.