ttocskcaj Posted February 19, 2012 Share Posted February 19, 2012 I'm trying to decide if I should load all my config options from a MySQL table into an array at the start of each execution, or if I should make a class to load a certain setting only when it is needed. Which one would be better? Loading them all at the start will save time on database queries, but it may use up a bit of memory or provide some security problems if I'm not careful how I treat the array. (Since it would include the MySQL creds etc). However if I made a class that could load individual settings from the table on the fly it would free up some memory and be secure because the data is never passed around the script. But this method would do a single database query every time a config entry is needed, which could be several times for every page. What would you suggest is the best method? Is there another way that I've missed? Quote Link to comment https://forums.phpfreaks.com/topic/257296-config-database-table-query-time-vs-memory-usage/ Share on other sites More sharing options...
kicken Posted February 19, 2012 Share Posted February 19, 2012 If your just talking about some config options, I'd say load them all at once. It probably won't take up that much memory (unless you have a ton of options) and having quick access too them would probably be best. You can setup a class for them which will load them all at what ever point the first option is required, so the loading is delayed until an option is needed. If you do have a lot of options too, you could perhaps break them up in to groups/categories and only load a particular group of options at a time. Quote Link to comment https://forums.phpfreaks.com/topic/257296-config-database-table-query-time-vs-memory-usage/#findComment-1318934 Share on other sites More sharing options...
scootstah Posted February 19, 2012 Share Posted February 19, 2012 How/why would you have database credentials IN a database? I usually make a simple function that has a static array of settings. On the first call it will load the settings from the database, and on subsequent calls it will just return it. It would look something like... function settings($setting) { static $settings = array(); if (empty($settings)) { // get settings from the database } return isset($settings[$setting]) ? $settings[$setting] : null; } Quote Link to comment https://forums.phpfreaks.com/topic/257296-config-database-table-query-time-vs-memory-usage/#findComment-1319000 Share on other sites More sharing options...
ttocskcaj Posted February 20, 2012 Author Share Posted February 20, 2012 How/why would you have database credentials IN a database? I normally just make an array called $config that already has the database settings. Then just add the settings from the database to it. Quote Link to comment https://forums.phpfreaks.com/topic/257296-config-database-table-query-time-vs-memory-usage/#findComment-1319121 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.