....snip.... my $query1 = "SELECT field1,field2,field3 FROM table WHERE `flag`=0"; my $sth = $dbh->prepare($query1); $sth->execute(); # Loop query to build an array # this pulls each row into an # array that we can manipulate while ( ($var1,$var2,$var3) = $sth->fetchrow_array() ) { # update the flag field to reflect the ad has been ehnanced my $query2 = "UPDATE table SET `flag`='1' WHERE field1=?"; my $sth2 = $dbh->prepare($query2); $sth2->execute($var1); ....snip.... }