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:[EMAIL PROTECTED]
> Sent: Wednesday, June 06, 2007 7:40 AM
> To: [email protected]
> Subject: [PHP] 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