Re: Random Unique ID
| From: | ccspencer at softhome dot net | Date: | Wed, 21 Mar 2007 22:40:46 +0000 |
| Subject: | Re: Random Unique ID | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-251053@lists.php.net to get a copy of this message | ||
Jim Moseby writes:
Thanks. Yes, I check for errors. But there are other types of errors so I'd need to verify that it is a duplicate key error and, in my ignorance, I have not yet figured out how to do that programatically. I worry about getting into an infinite loop. Best, Craig ------------------------------------------------------However, altho I know that by making the random number big enough the likelyhood of collisions can be made vanishingly small, I was actually concerned with eliminating the possibility of collisions altogether by checking to see if the number had been used before. I just don't know how to do that properly with Mysql. Perhaps it is necessary to lock to table, check, make the insert and then unlock it. But I was hoping that there would be a simpler way.One way is to make your id field a unique key. MySQL will not let you insert a record with a duplicate unique key, and will issue an error. Your code should always check for errors on insert anyway, so if you get an error, generate a new key and try again.
- Virtual Phonecards - Instant Pin by Email - - Large Selection - Great Rates - - http://speedypin.com/?aff=743&co_branded=1 - ------------------------------------------------------**************************************
* * * Craig Spencer * * ccspencer@SoftHome.net * * * **************************************