How can I store Sql query result in variable in perl?

Posted on 2008-11-02
Last Modified: 2012-06-21
I am executing following SELECT query from my perl program and I am using DBI interface
my $test=$dbh->prepare("SELECT AVG(diff) FROM data WHERE key1='s' and key2='i'");
$test-> execute();

The above query , if executed in SQL query browser generate average result of column 'diff' which I want to store in variable in perl for further use

I have idea of dump_results function provided by DBI as one of the option but doesn't seems to work for me
Question by:saini_er
  • 2
LVL 26

Expert Comment

ID: 22864908
Try this..

@row = $sth->fetchrow_array();
$myVar = $row[0];

Open in new window

LVL 26

Accepted Solution

ushastry earned 500 total points
ID: 22864911
oops...try this..

@row = $test->fetchrow_array();
$myVar = $row[0];

Author Closing Comment

ID: 31512593
Thanks a lot .it worked like a charm

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (  Today ( you would not use MySQL…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question