Michdd Posted September 11, 2009 Share Posted September 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 11, 2009 Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 So when changing a configuration via a control panel you completely rewrite the file which defines the constants? Quote Link to comment Share on other sites More sharing options...
trq Posted September 11, 2009 Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 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). Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 11, 2009 Share Posted September 11, 2009 well... your reading the database everytime your load the page.... you should store it in a session Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 11, 2009 Share Posted September 11, 2009 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']; } } } Quote Link to comment Share on other sites More sharing options...
corbin Posted September 11, 2009 Share Posted September 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 11, 2009 Share Posted September 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
l0ve2hat3 Posted September 11, 2009 Share Posted September 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 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.. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 11, 2009 Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Michdd Posted September 11, 2009 Author Share Posted September 11, 2009 Thanks for your input. I might consider writing a class myself for handling configuration loading. But probably not, it's easy to write useless classes, and I'm not even sure it's worth it. :s Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 11, 2009 Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.