I have a table of options in a database and I want to pull these out of the database and have them all be defined using the define(“DEF_NAME”, “DEF_VALUE”), but since they are pulled out of a database they do not have quotations marks around them.
this is the code I currently use:
$strOptionName = $rowDBOptions["strOptionName"];
$strOptionNameUpper = strtoupper($strOptionName);
$strOptionValue = $rowDBOptions["strOptionValue"];
$inttblOptionsType = $rowDBOptions["strOptionType"];
define($strOptionNameUpper, $strOptionValue);
The only issue is that I receive notices regarding undefined variables. Does anybody know of another option or method to create definitions from database variables?
Your code should work fine, my own personal method for extracting config from a database goes something like this:
Config database table has a ‘key’ ie.
GLOBAL_TAX_RATEand a value, ie.0.25Query looks like:
PHP (after database connection / query execution) looks like:
And you end up with a nice way to define all of your constants from the database without having to explicitly add a new statement if you want to add a new key/value pair.
And there are of course variations, such as on busy sites, storing these values in the session and looping through that – and every 10 minutes using a timestamp, or on login/logout, or whatever you need – refresh the config session values from the database to pick up any changes, without having to run a config SELECT every time someone opens a page.