Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help getting constants out of mysql 1

Status
Not open for further replies.

superslurpee

Programmer
May 15, 2002
108
Hi,

I am storing constant names in my mysql db and need to use them as constants in my php code and not just get them as strings. These constants are page names and locations already defined by the time the code is read.

So for a quick example, I have a constant defined as 'FILENAME_COMPANY' and it holds the path to that page. In the database, I want to store 'FILENAME_COMPANY' so that when I pull it from the db, it is recognized as the constant already existing and not just as a string.

I have tried eval but it still doesn't seem to recognize it as the constant.

Any ideas?

Darth Slurpee
--At last we will reveal ourselves to the SEV employees--
 
Given a MySQL table "foo" formatted as:

[tt]+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| Id | int(11) | NO | PRI | NULL | auto_increment |
| constant_name | varchar(30) | NO | | | |
| constant_value | varchar(30) | NO | | | |
+----------------+-------------+------+-----+---------+----------------+[/tt]

Containing the values:

[tt]+----+-----------------+--------------------+
| Id | constant_name | constant_value |
+----+-----------------+--------------------+
| 1 | COMPANY_NAME | Spacely Sprockets |
| 2 | HOME_DIRECTORY | /var/homes/spacely |
| 3 | CURRENCY | U.S. dollars |
| 4 | CURRENCY_SYMBOL | $ |
+----+-----------------+--------------------+[/tt]

Then the code:

Code:
<?php
function set_constants ()
{
	$dbh = mysql_connect ('localhost', 'test', 'test');
	mysql_select_db ('test', $dbh);

	$query = "SELECT * from foo";
	
	$rh = mysql_query($query, $dbh);
	while ($row = mysql_fetch_assoc($rh))
	{
		define ($row['constant_name'], $row['constant_value']);
	}
}

set_constants();

echo COMPANY_NAME;
?>

outputs:

[tt]Spacely Sprockets[/tt]


Is that what you were looking for?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi,

Thanks but that isn't exactly what I need. The constants already exist and have already been defined. The database isn't storing the information to define them, it just stores the name to use when it's needed. When I do the select, and echo the value, I want to get the contents of the constant, not the string name of it. For example, there might be a constant already defined as 'MY_COUNTRY' with a value of 'Canada'. The database holds 'MY_COUNTRY' and when selected, I want to echo the already defined constant value of 'Canada' and not the string 'MY_COUNTRY'. How do I tell php to take that string and associate it with the constant?

Thanks!

Darth Slurpee
--At last we will reveal ourselves to the SEV employees--
 
That is perfect! I'm surprised I didn't run across that in my searches. It seems like a pretty obvious function.

Thank you very much for your assistance. It is greatly appreciated!

Darth Slurpee
--At last we will reveal ourselves to the SEV employees--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top