mt2k has asked for the wisdom of the Perl Monks concerning the following question:
Okay, I have gotten MySQL and Perl working together for a CGI script. Everything is working fine, but I can't help but stare at the lengths I have to go through when I want to get a single value from the database. Below are two samples of code that work, but I am looking to see if a shorter way exists:
# Example 1: fetchrow_arrayref my $sth = $db->prepare("SELECT the_name FROM the_table WHERE id=3"); $sth->execute(); print $sth->fetchrow_arrayref()->[0]; $sth->finish(); # Example 2: fetchrow_hashref $sth = $db->prepare("SELECT the_name FROM the_table WHERE id=3"); $sth->execute(); print $sth->fetchrow_hashref()->{'the_name'}; $sth->finish();
I skipped the while{} loop for the fetchrow_hashref() because this is an instance where I know for a fact that there will only be one value returned. My real question is whether or not the value I am retrieving can be put in a variable during the call to prepare() (perhaps replacing it with do()). Something like this perhaps:
my $sth = $db->do("SELECT the_name FROM the_table WHERE id=3"); print $sth;
Thanks ahead of time (assuming you help me) :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: MySQL and Perl - Shorthand
by Zaxo (Archbishop) on Sep 21, 2002 at 03:26 UTC | |
by broquaint (Abbot) on Sep 21, 2002 at 13:06 UTC | |
Re: MySQL and Perl - Shorthand
by dws (Chancellor) on Sep 21, 2002 at 04:14 UTC | |
Re: MySQL and Perl - Shorthand
by George_Sherston (Vicar) on Sep 21, 2002 at 12:42 UTC | |
Re: MySQL and Perl - Shorthand
by schweini (Friar) on Sep 21, 2002 at 03:25 UTC | |
Re: MySQL and Perl - Shorthand
by Chady (Priest) on Sep 21, 2002 at 16:46 UTC | |
by chromatic (Archbishop) on Sep 22, 2002 at 01:28 UTC | |
Re: MySQL and Perl - Shorthand
by graff (Chancellor) on Sep 23, 2002 at 00:57 UTC |
Back to
Seekers of Perl Wisdom