fetchrow_array()
$sth->fetchrow_array()This statement handle method returns one row, the next from
the results of an SQL statement in the form of an array, each of whose
element is a field of data. Null values retrieved are returned as
undefined. An empty value is returned when there is an error or when
there are no more rows remaining in the results set. Therefore, if
used in a flow control statement such as while, the
empty value returned will end the loop statement. Here is an
example:
...
my $sql_stmnt = "SELECT title, author FROM books";
my $sth = $dbh->prepare($sql_stmnt);
$sth->execute();
while (my ($title, $author) = $sth->fetchrow_array(){
print "$title by $author \n";
}
$sth->finish();If you know that the SQL statement will return only one row, you
won’t need the while statement. Instead, you can
save the values directly into a tight list of variables:
...
my $sql_stmnt = "SELECT title, author
FROM books LIMIT 1";
my $sth = $dbh->prepare($sql_stmnt);
$sth->execute();
my ($title, $author) = $sth->fetchrow_array();
print "$title by $author \n";
...