You are here: Re: Error checking for unique value in database table? « PHP Programming Language « IT news, forums, messages
Re: Error checking for unique value in database table?

Posted by Erwin Moller on 05/23/07 08:21

Phil Latio wrote:

> I am inserting data into user table which contains 5 fields, sounds simple
> enough normally but 2 of the fields are designated as UNIQUE. If someone
> does enter a value which already exists, how do I capture this specific
> error?

Hi,

You capture the error by using the errormessage on your connection.
How the function is named depends on the database you are using, or the
database abstraction layer.

>
> Would it make more sense to actually run a SELECT query first and if that
> returned a result, then I use that for error checking and don't run insert
> until select returns nothing?

Yes.
I would say that is the right way of doing it with style.

You could of course try to parse your errormessage and look for substrings
like 'UNIQUE CONSTRAINT VIOLATION' (just an example), but that message
could very well be different if you switch databases, or upgrade the one
you use now.

So I would go with the general solution you described yourself: check first
for violation of UNIQUE constraint, then insert.

Regards,
Erwin Moller

>
> Cheers
>
> Phil

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация