Re: Re: Random Unique ID
| From: | Richard Lynch | Date: | Sat, 24 Mar 2007 01:48:46 +0000 |
| Subject: | Re: Re: Random Unique ID | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-251254@lists.php.net to get a copy of this message | ||
On Wed, March 21, 2007 5:40 pm, ccspencer@softhome.net wrote:
> 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.
Untested code:
//create table test_id (id char(32) unique not null primary key));
$success = 0;
while (!$success){
$id = md5(uniqid());
$query = "insert into test_id (id) values('$id')";
$result = mysql_query($query);
if (!$result){
//I don't promise 1023 is right.
//force a duplicate as a test to confirm
if (mysql_errno() == 1023){
//duplicate key
//we'll loop back and try with another
}
else{
echo "<p>Something went wrong with our database. Sorry.";
error_log(mysql_error());
break;
}
}
else{
$success = 1;
}
}
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?