You are here: Re: is this okay? « PHP Programming Language « IT news, forums, messages
Re: is this okay?

Posted by Andy Hassall on 10/22/05 14:09

On 21 Oct 2005 17:58:41 -0700, "siliconmike" <siliconmike@yahoo.com> wrote:

>pseudocode:
>
>function generate_unique_id()
>{
> $id = 10 digit random number;
> make sure that $id does not exist in our table
> if it does, generate a new one
>
> insert $id into our table;

You mention a "table", so I assume this is going into a database.

If so, simply add a unique constraint to the column, and leave the locking to
the database.

Don't check if it exists with a SELECT since that introduces a race condition;
attempt the INSERT, and catch the unique key violation if there is one.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

 

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

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