Mohan
Mohan

Reputation: 473

Fetch data with one row and one column from table using Perl DBI

I am trying to fetch data like (Select 1 from table) which returns data with one row and one column.

I dont want to use $sth->fetchrow_array method to retreive the data in to array. Is there any way to collect the data into scalar variable direclty?

Upvotes: 5

Views: 18955

Answers (2)

ikegami
ikegami

Reputation: 385897

fetchrow_array returns a list —it's impossible to return an array— and you can assign that to anything list-like such as a my().

my $sth = $dbh->prepare($stmt);
$sth->execute();
my ($var) = $sth->fetchrow_array()
   and $sth->finish();

Or you could simply use

my ($var) = $dbh->selectrow_array($stmt);

Upvotes: 10

Jonathan Warden
Jonathan Warden

Reputation: 2502

my ($value) = @{$dbh−>selectcol_arrayref("select 1 from table")}

or better

my ($value) = $dbh−>selectrow_array($statement);

Upvotes: 1

Related Questions