http://qs1969.pair.com?node_id=246500


in reply to Re: Constant Variables
in thread Constant Variables

I think this might work (though I'm at school and I can't test it):
$dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); my %settings = %{$sth->fetchrow_hashref}; # I'm assuming you only + have one row... foreach my $key (keys %settings) { my $varname = "$" . $key; eval "$varname = $settings{$key}" or die "problem with eval"; } $sth->finish();
Also, from what you've said, it seems as if you are just using the settings to globally control the properties of HTML text so you only have to change the property in one place. If this is the case, then you should use CSS (Cascading Style Sheets) to define site-wide styles and include the CSS file in every page. CSS is a very powerful mechanism for easily changing the layout and design of a site.

Update: Hmm, I just saw that you want to use the values in the column name as the variable name. The code above uses the name of the field (or column) in MySQL as the variable name. Here is the code to do what you want:

$dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my %settings = %{$sth->fetchrow_hashref};) { my $varname = "$" . $settings{name}; eval "$varname = '$settings{value}'" or die "problem with eval"; } $sth->finish();
That should do the trick, but it is also untested.