How to store Site Configuration in Database

I am glad that after initial launch of my blog, I stumbled upon a code in my project which is worth sharing. I created function which will use database to store site configurations.

Recently, I got an email from my one of my client on whose project I am currently working. She enlisted a number of site settings which she wanted to be able to manage through the admin panel of the site. It would have been easy if it was one or two settings. But there were dozens of them. Now I had two options:

1. Create a php function which will edit php files and alter the variables in them
2. Manage it by storing the site configurations in database.

Well, I went with the second option because the first one was not feasible and will create a mess in long run. Because the php function will be utilized by pointing it to line number of the variable in a specific file. But what if another developer changes that php file and so does the line number of that variable?
So I went with the second option as follow.

1. Creation of a table in database:

Creating a table with columns named after all those settings variable was not an option because these settings are expected to expand in future and adding more columns to the table after that will not only make the table structure very big but will also effect performance as this table is expected to be queried on every page load.
So first of all, I created a table named “settings” which will store the site’s configuration in database. This table had three columns:


As its obvious from the table, the name field will store the name of the settings variable while the value column will store the value of that name field.

You can see that there are a number of variables and their values shown in the sample table. After this, I was put into the thought that how to retrieve a specific setting’s value from the database because there was no column for the setting named variables and using a foreach() loop and comparing the variable name with that of the retrieved data was not an option.

2. Custom Function to turn the data into array

To solve this problem, I created a function which will turn the whole data of the settings table into an array. With name as key and value as value of that key.


I have used simple mysql functions but its not appreciated to be used. I just used them to simplify the function. I was developing the code in Codeignitor and I used framework specific functions.

So let’s explain it. First of all, I retrieved all the data from the settings table and stored it in a variable $results. Then I created an empty array named $settings. Then I used the $results in the foreach() loop and created keys by using $key[‘name’] and then sat the value of that specific key by using $key[‘value’].

As foreach() loop reads one array at a time, so it ensured that the value of every $key[‘name’] will be set to the corresponding value from the value column of database. On completion of the loop, we had an array named $settings.

Now to simply use the settings for site configuration, I will do the following:

Using this array, you can easily use an if elseif statement to check the settings and do perform appropriate actions in your code. You can easily update the above settings using a mysql query from admin panel. Some points to be note:

  • Use Human Readable data in name and value columns.
  • Use lower case for all the stored data so that your script do not output unexpected error
  • Use Boolean values if possible

I hope that this might help you in your projects. I saw it once in WordPress. A table store the site configuration in database and use something similar to the above function to utilize that data. If you have a better idea to utilize site’s configuration stored in database, then please share it with us in your comments below. Also if you have any question you can ask, Happy commenting : )