I have a table of options in a database and I want to pull these out of the database and have them all be d开发者_如何学编程efined 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_RATE
and a value, ie. 0.25
Query looks like:
SELECT conf_key, conf_value FROM config_options
PHP (after database connection / query execution) looks like:
while ($ROW = mysql_fetch_assoc($QRY)) {
define($ROW['conf_key'],$ROW['conf_value']);
}
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.
Your code example should work fine. Please check which line(s) are reported for the notices; it seems that your script relies on a constant that is not in the database.
精彩评论