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 Jerry Stuckle on 05/23/07 10:49

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?
>
> 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?
>
> Cheers
>
> Phil
>
>
>
>
>

Phil,

Look at the error code returned by the database.

Most databases return an error code and an error message. The error
message is great for humans; the error code better for computers, and
should be unique (IIRC, this one is 1062 for MySQL).

Don't do a select first. It's unnecessary overhead and not guaranteed
unless you lock the tables first (more overhead). For instance, you
might search for "john" and not find it - but before you can add "john",
another process adds it. Now when you try, you get a duplicate, even
though you checked first.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

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

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