Re: Re: Random Unique ID

From: Date: Wed, 21 Mar 2007 21:15:13 +0000
Subject: Re: Re: Random Unique ID
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-251049@lists.php.net to get a copy of this message
ccspencer@softhome.net wrote:
Stut writes:
ccspencer@softhome.net wrote:
I want to add a random unique ID to a Mysql table. Collisions are unlikely but possible so to handle those cases I'd like to regenerate the random ID until there is no collision and only then add my row. Any suggestions for a newbie as to the right way to go about doing this?
1) Not even slightly PHP related.
Perhaps not. It is Mysql related. And seeing other Mysql discussion on this list inspired me to ask.
I refer you to the comment made earlier today by one of my honourable colleagues regarding murder.
2) Why random? Incrementing not good enough for you?
I want to use it in a way that will expose it to the user. And I don't want to give out information as to the order of records in my database.
This is not security. Your site should prevent them from accessing data they should not be accessing. If you're worried that they can do this simply by changing a number in a query string then you really need to rethink the way the site works. If you decide to ignore my advice, then the only way to do this is to set the ID field as unique, pick a random number and try to insert the row. Repeat until the insert succeeds. Yes it sucks, but so does what you are trying to do. -Stut

« previous php.general (#251049) next »