• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1430
  • Last Modified:

Returning a single value from resultset in Perl DBI

My result set is going to return only one value from the database. I am connecting to the DB using perl dbi. How shd i fetch the value? Please advise.
1 Solution
Carl BohmanCommented:
Something like this should work (untested)
use DBI;
my $dbh = DBI->connect(...) or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare(...) or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute() or die "Couldn't execute statement: " . $sth->errstr;
($sth->rows > 0) or die "No rows matched.";
my $Data = ($sth->fetchrow_array())[0];

Open in new window

Do you have some code that you've already tried but doesn't work? If so, post it here please.

You can use prepare/exectue/fetch as shown by bounsy, or you can just use one of the selectrow methods, which does all of these at once.
#Get data as array
my @record = $dbh->selectrow_array('SELECT ....');
#Get data as arrayref
my $record = $dbh->selectrow_arrayref('SELECT ....');
#Get data as hashref
my $record = $dbh->selectrow_hashref('SELECT ....');

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now