in reply to database selecting then validating

my $sql = 'select count(*) from users where empid=? and dept=?'; my ($count) = $dbh->selectrow_array($sql,undef,$empid,$dept); if ($count == 1){ # valid } else { # not valid }
poj

Replies are listed 'Best First'.
Re^2: database selecting then validating
by Anonymous Monk on May 25, 2012 at 11:33 UTC

    You could simplify that into (assuming a unique constraint on (empid, dept)):

    my $sql = 'select 1 from users where empid=? and dept=?'; if ($dbh->selectrow_array($sql, undef, $empid, $dept)) { # valid } else { # not valid }

    ...I'm always a bit wary of using Count(*) for exists-or-not-exists queries