How can I efficiently retrieve a large number of database settings as PHP variables?

Posted by Steven on Stack Overflow See other posts from Stack Overflow or by Steven
Published on 2010-03-28T11:08:58Z Indexed on 2010/03/28 11:13 UTC
Read the original article Hit count: 160

Filed under:
|
|
|

Currently all of my script's settings are located in a PHP file which I 'include'. I'm in the process of moving these settings (about 100) to a database table called 'settings'. However I'm struggling to find an efficient way of retrieving all of them into the file.

The settings table has 3 columns:
ID (autoincrements)
name
value

Two example rows might be:
1
admin_user
john

2
admin_email_address
[email protected]

The only way I can think of retrieving each setting is like this:

$result = mysql_query("SELECT value FROM settings WHERE name = 'admin_user'");
$row = mysql_fetch_array($result);
$admin_user = $row['value'];

$result = mysql_query("SELECT value FROM settings WHERE name = 'admin_email_address'");
$row = mysql_fetch_array($result);
$admin_email_address = $row['value'];

etc etc

Doing it this way will take up a lot of code and will likely be slow.

Is there a better way?

Thanks.

© Stack Overflow or respective owner

Related posts about database

Related posts about settings