Jump to content

Archived

This topic is now archived and is closed to further replies.

fishtank22

6 Character Alphanumeric Unique ID

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.

Share this post


Link to post
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\" /]

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Thanks... Yeah, I kept in simple for the example here, but thats what I was thinking. I'm gonna give it a shot now. thanks a lot guys. I really appreciate it.

Fish

Share this post


Link to post
Share on other sites

×

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.