RE: customer ids using UUID, MD5, reverse logic, error 1062

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Have you thought of using a UUID:
http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#func
tion_uuid

While not real pretty, it would save you the extra "SELECT".
You could also just MD5(UUID()) to make it a bit 'smaller', or some
other mechanizm.

Even something as simple as MD5(UNIX_TIME()) might be fine enough
resolution/granularity for your needs, depending on how many
simultaneous "INSERT"s you expect. 

Honestly, I would use a little reverse logic, and not do the select
first (which is a wasted query as you have more chance of success if
your 'random' pool is big/unique enough), but rather _assume_ it will
INSERT, and then handle the case you get a "DUPLICATE KEY" issue (i.e. a
failure. "ERROR 1062 (23000): Duplicate entry '1' for key 1", so you can
check:

This is rough pseudo code, but you get the idea...

$key = UUID();
While (!success)
{
 $sth = mysql_query("insert into () values ()");
 if (!$sth || mysql_error() == '1062')
 {
	//pick a new key and loop around.
      continue;
 }
 $success = true;
}

> -----Original Message-----
> From: blueboy [mailto:ross@xxxxxxxxxxxxx] 
> Sent: Wednesday, June 06, 2007 7:40 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  customer ids
> 
> Hi,
> 
> I want to create random customer ids. I have an auto 
> incremented coulumn but 
> I would rather have a 6-8 digit/letter id randomly generated  
> that can be 
> used as a unique identifier across 3 tables. Does anyone have 
> a algorithm to 
> generate such a string and can you give the odds against 2 
> duplicate stings 
> being generated?
> 
> I know this is a strange ask.
> 
> 
> R. 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux