Jump to content

Basic Loop Question


mattichu
Go to solution Solved by Psycho,

Recommended Posts

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!

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.