Dr.Avalanche
Dr.Avalanche

Reputation: 1996

Perl - CGI::Application - Create session variables from database

I have a bunch of configuration variables stored within a database, key value pairs accessable via the following query:

select * from conf_table;

I want to load these key/value pairs into a CGI::Applicaiton session. At the moment this is manually done (so not from database, but hardcoded) via

$self->session->param( NAME => VALUE );

For a bunch of key value pairs. Is there a more sensible way do to this with DBI and some form of loop?

Thanks

Upvotes: 1

Views: 235

Answers (2)

wes
wes

Reputation: 8175

DBI has some convenience methods that make this sort of work simpler. Try selectall_arrayref:

my $configs = $dbh->selectall_arrayref(
    'SELECT * FROM conf_table',
    { Slice => {} }, # make each row a hash
);
$self->session->param($_->{key} => $_->{value}) for @$configs;

Upvotes: 1

Dave Sherohman
Dave Sherohman

Reputation: 46187

You mean something like this?

my $sth = $dbh->prepare("select key, value from mytable");
$sth->execute;
$sth->bind_columns(\(my ($key, $value)));
while ($sth->fetch) {
  $self->session->param($key => $value);
}

Upvotes: 3

Related Questions