Jump to content


Photo

6 Character Alphanumeric Unique ID


  • Please log in to reply
5 replies to this topic

#1 fishtank22

fishtank22
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 June 2006 - 03:59 AM

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:

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

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?

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();
   }
}

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.

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 04:06 AM

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\" /]
~ D Kuang

#3 fishtank22

fishtank22
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 June 2006 - 04:08 AM

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) View Post[/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]


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 14 June 2006 - 04:15 AM

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:
  if (!$numResults) {
      generateUniqueID();
   } else {
      return $token;
  }

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 04:24 AM

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

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

You should also make the "code" column UNIQUE so MySQL won't INSERT a duplicate even if your script fails to verify it.
~ D Kuang

#6 fishtank22

fishtank22
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 14 June 2006 - 04:25 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users