Will wrote:

I am developing a registration area for a members site
that will interface with a MySQL DB users table, and I
ran into a problem.  Note that I am using DBI
as my DB Driver.

Suppose the user tries to register with a username
that has already been taken.  I need a way to kick
back an error message to them.  I tried setting the
username field in the usrs table to UNIQUE, so that
might help if someone tried to insert something
already taken... I was thinking that if MySQL kicks
back an error message, then DBI might be able to
recongize it in such a way that I could use the return
value... I dont know if that is completely feasible
though...  there may be other, better ways anyway...
so I'm all ears...
If you know the errors you want to trap and handle you can
eval the SQL operation.  e.g., I have code for Oracle that
tries to create a temporary table but doesn't care if it
already exists.  That looks like this (snippet):

   eval
   {
       $dbh->do($sql);
   };

   die $@ if ( $@ && ($@ !~ /ORA-00955/) );

In other words, I continue if the error was one telling me
the table exists.  You can flip that around and do something
special for recognized errors.

You don't have to use eval.  If you read the DBI docs on
handling errors you can do it other ways by checking return
codes and error values/strings.

--
Steve Sapovits    GSI Commerce, Inc.    http://www.gsicommerce.com
Email:   [EMAIL PROTECTED]


Reply via email to