0

I recently figured that there is a selectrow_array function to fetch values from databases. I'm getting following error when I'm using it. I wonder what is the issue here and couldn't find an alternative way to do this.

Code is:

my $db_connection = DBI->connect($dsn, $dbuser, $dbpassword ) or die $DBI::errstr;

my $sql_statement = "SELECT customer_id,quota FROM customer_call_quota WHERE quota>=1";

while (my $row = $db_connection->selectrow_array($sql_statement)) {
     my ($cust_id, $quota) = @$row; #<---- error line
 }

my $rc = $db_connection->disconnect ;
return "ok";

Error:

Can't use string ("value") as an ARRAY ref while "strict refs" in use at code.pl line ...

1 Answer 1

2

Two problems.

  • selectrow_array doesn't return a reference to an array. That's selectrow_arrayref.
  • selectrow_* only returns the first row.

Solutions:

# Wasteful

my $sth = $dbh->prepare($sql_statement);
$sth->execute();
while (my @row = $sth->fetchrow_array()) {
    my ($cust_id, $quota) = @row;
    ...
}

or

my $sth = $dbh->prepare($sql_statement);
$sth->execute();
while (my ($cust_id, $quota) = $sth->fetchrow_array()) {
    ...
}

or

my $sth = $dbh->prepare($sql_statement);
$sth->execute();
while (my $row = $sth->fetch()) {
    my ($cust_id, $quota) = @$row;
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, its only returns the first row. Thanks for the answer!
Yes, but it's actually the other problem that resulted in the error you got

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.