katlis Posted December 7, 2008 Share Posted December 7, 2008 Hey guys, I'm using the following code to generate a random string: function generatePass ($length) { $password = ""; $possible = "0123456789bcdfghjkmnpqrstvwxyz"; $i = 0; while ($i < $length) { $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); $password .= $char; $i++; } return $password; } I need it to check a table though to make sure it doesn't generate one that already exists in my randomID column. Any ideas on how to go about this, having it re-generate the string if it already exists (then checking again, etc)? Quote Link to comment https://forums.phpfreaks.com/topic/135932-solved-generate-a-random-string-that-doesnt-exist-in-db/ Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 You need your DB connection inside the function, than use a while loop, while alreadyExists is true continue, set alreadyExists to false when you know it is not in your DB. Hope that logic helps to get you going. Quote Link to comment https://forums.phpfreaks.com/topic/135932-solved-generate-a-random-string-that-doesnt-exist-in-db/#findComment-708571 Share on other sites More sharing options...
katlis Posted December 7, 2008 Author Share Posted December 7, 2008 Thanks premiso. I sort of understand what you're saying... but I'm really not sure where to start the while loop or how to set alreadyExists to false if it isn't in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/135932-solved-generate-a-random-string-that-doesnt-exist-in-db/#findComment-708577 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 function generatePass ($length) { $alreadyExists = true; while ($alreadyExists) { $password = ""; $possible = "0123456789bcdfghjkmnpqrstvwxyz"; $i = 0; while ($i < $length) { $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); $password .= $char; $i++; } $sql = "SELECT random FROM tbl_name WHERE random = '" . $password . "'; $res = mysql_query($sql); if (mysql_num_rows($res) == 0) { $alreadyExists = false; } } return $password; } Hope that helps. You will have to modify the SQL and make sure you do have a SQL connection for that work. Quote Link to comment https://forums.phpfreaks.com/topic/135932-solved-generate-a-random-string-that-doesnt-exist-in-db/#findComment-708580 Share on other sites More sharing options...
katlis Posted December 7, 2008 Author Share Posted December 7, 2008 premiso, thanks a bunch, I see now! One thing though, if anyone else uses the code above, just fix one typo (missing end quote): $sql = "SELECT random FROM tbl_name WHERE random = '" . $password . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/135932-solved-generate-a-random-string-that-doesnt-exist-in-db/#findComment-708616 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.