Jump to content

6 Character Alphanumeric Unique ID


fishtank22

Recommended Posts

Hey everyone....

I have a project where I add new records into MYSQL. Along with the standard autoincrement unique id, I also would like to have a 6 digit alphanumeric code that is associated with each record (which would also need to be unique.

I have the script to create the unique id:

[code]$token = substr(md5(uniqid(rand(), true)),0,6);  // creates a 6 digit token[/code]

I would need to query the DB to ensure that the number hasn't been used already. Sure its like a billion to one, but its the right thing to do. My question is. If the value is taken, can I call the same function again from within itself, or is that bad coding?

[code]
function generateUniqueID () {
   $token = substr(md5(uniqid(rand(), true)),0,6);  // creates a 6 digit token
   $query = "SELECT count(*) FROM table WHERE code = $token";
   $result = mysql_query($query, $connection) or die(mysql_error());
   $numResults = mysql_num_rows($result);
   if (!$numResults) {
      generateUniqueID();
   }
}
[/code]

Also, if your wondering why I dont go with the entire 32 character md5 string. Its because the code needs to be short enough for people to be able to enter it easily.

Any guidance would be appreciated.
Link to comment
Share on other sites

That is recursion, and surely I don't consider it bad coding.
Indeed, I've always found recursive functions beautiful [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Link to comment
Share on other sites

Really? Huh, I thought that was a problem. I can see in this case the chance of it even looping though more than 1x is rare, but there are some functions where this can be a problem. Or maybe I'm just paranoid.

Thanks for the feedback!

[!--quoteo(post=383608:date=Jun 14 2006, 12:06 AM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 14 2006, 12:06 AM) [snapback]383608[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That is recursion, and surely I don't consider it bad coding.
Indeed, I've always found recursive functions beautiful [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
[/quote]
Link to comment
Share on other sites

looks alright to me. you might have to throw a

global $connection;

so that the function can use that variable. also I'm assuming that you are calling this function from somewhere and then inserting the code into the table, like

$code = generateUniqueID ();

so you are going to have to return $token; in your function, like this:
[code]
  if (!$numResults) {
      generateUniqueID();
   } else {
      return $token;
  }
[/code]
Link to comment
Share on other sites

Oh, that made me notice that there is a logical flaw with your code. This should do:

[code]function generateUniqueID () {
   $token = substr(md5(uniqid(rand(), true)),0,6);  // creates a 6 digit token
   $query = "SELECT count(*) FROM table WHERE code = $token";
   $result = mysql_query($query, $connection) or die(mysql_error());
   if (mysql_num_rows($result) !== 0) {
      generateUniqueID();
   } else {
      return $token;
   }
}[/code]

You should also make the "code" column UNIQUE so MySQL won't INSERT a duplicate even if your script fails to verify it.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.