mattichu Posted February 17, 2015 Share Posted February 17, 2015 Hi Probably a simple solution to this: I need to check if a randomly generated string is already in the database, if it is I need to generate another one until a unique one is found. How do I go about implementing this? I have the following code: <?php $name=$_POST['name']; $mobile=$_POST['mobile']; $email=$_POST['email']; $name=ucwords($name); $email=strtolower ($email); function randStrGen($len){ $result = ""; $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $charArray = str_split($chars); for($i = 0; $i < $len; $i++){ $randItem = array_rand($charArray); $result .= "".$charArray[$randItem]; } return $result; } $randstr = randStrGen(5); $code=$randstr; $servername = "********"; $username = "******"; $password = "******"; $dbname = "******"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM freedrink WHERE code='$code'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Need a loop somehow } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/294686-basic-loop-question/ Share on other sites More sharing options...
Solution Psycho Posted February 18, 2015 Solution Share Posted February 18, 2015 There are a few problems with trying to do this. I assume if you find a value that is not already in use, you plan to create a new record with that value. Well, there is something called "race conditions". There is the possibility that between the time you check if the value is used and the time you try to INSERT the value another user has trigger the same process and used that value. In your particular scenario it would seem very unlikely if you have a sufficiently large enough pool of values to choose from. But, depending on how "random" the value really is, it can actually be possible. If you were to search this forum you would find answers to this problem, but they don't scale well. How are you planning to use this value such that it has to be a unique random value? For example, if you were going to use it in a link sent in an email to reset a password, you would want to make sure two users never get the same value. But, you could just create a 'random' string and then append the ID of the user onto the random string. Then it would be impossible for two users to ever have the same value. Not knowing your exact use case, I don't know if there would be a better alternative solution or not. https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=php%20freaks%20random%20value%20database Quote Link to comment https://forums.phpfreaks.com/topic/294686-basic-loop-question/#findComment-1505970 Share on other sites More sharing options...
mattichu Posted February 18, 2015 Author Share Posted February 18, 2015 Hi Thanks that makes sense, I am using the system to email unique voucher codes upon the submission of customer details. I have chosen to append the database record ID to the beginning of the code so that they can never be the same. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/294686-basic-loop-question/#findComment-1505972 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.