create table prefs ( user_login text, color1 text, color2 text, fontsize text, ... , primary key (user_login) );
####
use constant DS => 'my dsn here';
# later ...
my $dbh=DBI->connect(DS);
my($color1, $color2, $fontsize);
eval {
my $sth=$dbh->prepare("SELECT color1, color2, fontsize, ... FROM prefs WHERE user_login=?");
$sth->execute($login);
$sth->bind_columns( \( $color1, $color2, $fontsize ) );
$sth->fetch;
$sth->finish; # using primary key to only fetch 1 row
};
if($@)
{
print STDERR "Informative error message goes here with $@ for good measure\n";
}
##
##
#change lines from above
my($color1 ...); #becomes
my $prefs;
#bind_columns becomes
$sth->bind_columns( \my( $c1, $c2, $fs ) );
$prefs={ color1 => $c1, color2 => $c2, fontsize => $fs };
#later to get a pref
print "My favorite color is $prefs->{color1}\n";
##
##
create table prefs ( user_login text, prefname text, prefvalue text, primary key ( user_login, prefname) );
##
##
my %prefs;
...
eval {
my $sth=$dbh->prepare("SELECT prefname, prefvalue FROM prefs WHERE user_login=?";
$sth->execute($login);
$sth->bind_columns( \my( $n, $v ) );
while($sth->fetch)
{
$prefs{$n}=$v;
}
};
if($@)
{
#as before
}
#later to use
print "My favorite color is $prefs{color1}\n";
# or even
foreach my $p ( keys %prefs )
{
print "I like my $p to be $prefs{$p}\n":
}