... $dbh = DBI->connect("DBI:mysql:$database:$hostname", $user, $password); $statement = "select ... order by ..."; $sth = $dbh->prepare($statement) or die "Can't prepare $statement: $dbh->errstr\n"; $rv = $sth->execute or die "can't execute the query: $sth->errstr\n"; my @row; while(@row = $sth->fetchrow_array) { # @row contains the individual columns # ... do work ... } $rc = $sth->finish; $rc = $dbh->disconnect;