SPUG: Using a Duplicate Value Error Message

Colin Meyer cmeyer at helvella.org
Tue Mar 20 10:20:06 PDT 2007


On Tue, Mar 20, 2007 at 08:53:10AM -0700, Fred Morris wrote:
> Hello Will, welcome to the list!
> 
> Without critiquing style (doesn't this come down to style?), isn't the answer 
> obviously that what you'd normally do at that point in the code is display 
> whatever congratulations page you're planning to send to the user for 
> registering? You have to send something to the user as a success page if they 
> are successful.
> 
> FIRST: You have to test $sth for success, that is to say
>   $an_error = (! $sth);

Note that $sth will have a false value only if $dbh->prepare( $sql )
failed. If $sth was created successfully, but an error occurred during
$sth->execute(), then $sth will still hold a true value (which is the
statement handle object).

Also, Will turned on the RaiseError attribute in $dbh, at DBI->connect
time. This means that the DBI will throw an exception for any error,
causing the code to be interrupted before having the opportunity to
check for return values.

The proper way to trap DBI RaiseError exceptions (and other Perl
exceptions) is an eval block, as BenRifkah described this in his reply.
My only addition is that I would include the $sth = $dbh->prepare()
statement in the eval block (or perhaps wrap it in its own eval block),
to trap any error that may occurred during statement preparation.

hth,
-Colin.


More information about the spug-list mailing list