Re: Re: Random Unique ID

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

 



On Wed, March 21, 2007 5:40 pm, ccspencer@xxxxxxxxxxxx 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?

-- 
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