Jump to content

Recommended Posts

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?

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.

 

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

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.