my $found_id = undef; my $sql = <prepare( $sql ); if ( $sth && $sth->execute( $url ) ) { if ( my $row = $sth->fetchrow_arrayref() ) { $found_id = $row->[0]; } } else { die( "Database error " . $dbh->errstr . "\n" ); } if ( $found_id ) { # perform update $sql = <prepare( $sql ); if ( $sth && $sth->execute( $last_ip, time(), $found_id ) ) { if ( my $row = $sth->fetchrow_arrayref() ) { $found_id = $row->[0]; } } else { die( "Database error " . $dbh->errstr . "\n" ); } } else { # perform insert # code left to read as an exercise }