fishtank22 Posted June 14, 2006 Share Posted June 14, 2006 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. Quote Link to comment Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 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 More sharing options...
fishtank22 Posted June 14, 2006 Author Share Posted June 14, 2006 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] Quote Link to comment Share on other sites More sharing options...
.josh Posted June 14, 2006 Share Posted June 14, 2006 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] Quote Link to comment Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 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. Quote Link to comment Share on other sites More sharing options...
fishtank22 Posted June 14, 2006 Author Share Posted June 14, 2006 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.