in reply to Re: Eval not working
in thread Eval not working

Thanks Utilitarian for explaining that.

Am I right in saying the only way to catch a problem like that would be to use the 'affected rows' function?

Replies are listed 'Best First'.
Re^3: Eval not working
by ikegami (Patriarch) on Sep 03, 2010 at 15:52 UTC
    $dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; my $id = 737; my $rows; if (!eval { my $sql = "UPDATE stockhistory SET invoiced = 1 WHERE id = ?"; my $sth = $dbh->prepare($sql); $rows = $sth->execute($id); $dbh->commit(); 1 }) { print "Transaction aborted because $@"; $dbh->rollback(); } die("Record $id not found") if $rows < 1;

    Note: Can't check if $rows is true since "0E0" (equal to zero but true) is returned when no rows are affected.

      Thanks for all your help monks.
      I'll re-write my script to include your suggestions.
Re^3: Eval not working
by TomDLux (Vicar) on Sep 03, 2010 at 14:26 UTC

    Exactly.

    Your query might successfully update every record in the table, or only one, or none at all, but it ran successfully. The number of rows affected is provided by:

    print 'The query updated '. $sth->rows . " rows.\n";

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.