Ask Any Question

PHP: How to store system configurations on a database and use them as constants?

robotchicken - 2008-11-06 10:31:33 - Programming Design

I'd like to have constants in PHP which are retrieved from a database. Essentially, I'd like to create a page that can update this database table so the next time I load the site, it will use these newly updated constants which are again retrieved from the database. I'm looking for things that can be changed directly by the admin without needing to go into the source code and manually changing the hardcoded constants file.


Best Answer:

Make a settings file and put this in it (settings.php) <?php $cfg_server="serveraddress"; $cfg_database="dbname"; $cfg_username="username"; $cfg_password="dbpassword"; ?> Now just put this in any file that needs to access the database include ("settings.php"); $link = mysql_connect("$cfg_server","$cfg_username","$cfg_password") or die("error "); mysql_select_db ("$cfg_database", $link) or die("error ");

Answers:

wefixcomputers.net - 2008-11-06 10:51:01
Make a settings file and put this in it (settings.php) <?php $cfg_server="serveraddress"; $cfg_database="dbname"; $cfg_username="username"; $cfg_password="dbpassword"; ?> Now just put this in any file that needs to access the database include ("settings.php"); $link = mysql_connect("$cfg_server","$cfg_username","$cfg_password") or die("error "); mysql_select_db ("$cfg_database", $link) or die("error ");

Chris C - 2008-11-06 11:48:47
Define a table with two columns: CREATE TABLE IF NOT EXISTS myconfigtable ( valueName varchar(50) collate utf8_unicode_ci NOT NULL value varchar(50) collate utf8_unicode_ci NOT NULL ); Then, fetch the necessary records from the database by simple SQL: SELECT valueName, value FROM myconfigtable WHERE valueName = "myconfigparameter1" ETA: The other option is to place a PHP file that is included within your web pages into a directory where it can be modified by using the fopen(), readfile(), writefile() functions already available in PHP

Visit: map google sitemap
© 2007 Love2Run.com