in reply to Single mysql query
Calling fetch only once is fine if you only need the first row. Whenever you are done with the statement handle, just call finish:
However, I would suggest not binding columns and, instead, use fetchrow_hashref:my ($id, $title); my $sth = $dbh->prepare("SELECT id, title FROM images WHERE id > ? LIM +IT 1"); $sth->bind_columns(\$id, \$title); $sth->execute($some_value); $sth->fetch; $sth->finish;
This approach also makes it easier to determine if the query returned no rows - a case that your code should be written to handle.my $sth = $dbh->prepare(...); $sth->execute(...); my $row = $sth->fetchrow_hashref; unless ($row) { # no rows returned } else { # process $row } $sth->finish;
|
|---|