http://www.perlmonks.org?node_id=199692

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) :)