-2

I have this PHP code, that will generate a random 6 letter ordernumber:

<?php 
$random = mt_rand(100000,999999); 
echo $random;
?>

I can insert this number in my database, but how do I make sure that there is no dublicates, and the ordernumber is unique?

5 Answers5

0

You can make your database column as Unique. Also for double check before inserting check if that column does already have that value.

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
0

You should simply create UNIQUE INDEX over this field in database. Generate number, check if it exists in db, if yes, try again until it does not.

The only catch that you should make sure that your address space for random keys is not too populated, otherwise generating new key without collision will become too slow.

As long as number of rows is less than say 10% of the possible address space, this method should work well. But with 50% or more it will be rather bad.

You can also create some inventive schemes that try to keep list of unused keys so far, but this is probably too complicated.

mvp
  • 111,019
  • 13
  • 122
  • 148
0

Set the number as a unique index and catch an error where a duplicate is found. Or just use the MySQL function RAND() to generate the number instead, then retrieve the value.

You could possibly do it in a single INSERT statement. Something like this (although this particular example slows down dramatically as the number of rows increases)

INSERT INTO numbers (SomeNumber)
SELECT aNum
FROM (SELECT a.i+b.i*10+c.i*100+d.i*1000+e.i*10000 AS aNum
FROM integers a, integers b, integers c, integers d, integers e) Sub1
LEFT JOIN Numbers ON Sub1.aNum = Numbers.SomeNumber
WHERE Numbers.SomeNumber IS NULL
ORDER BY RAND()
LIMIT 1

Or a lot more efficient, but leaves the possibility that no row will be inserted (it generates 10 random numbers and picks one that is unused to insert), hence you would need to check the rows affected and if 0 try again:-

INSERT INTO numbers (SomeNumber)
SELECT aNum
FROM (SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum) Sub1
LEFT JOIN Numbers ON Sub1.aNum = Numbers.SomeNumber
WHERE Numbers.SomeNumber IS NULL
LIMIT 1

With both these examples you could retrieve the id of the inserted row and then from that select the number inserted.

Kickstart
  • 21,403
  • 2
  • 21
  • 33
0
<?php 
$random = mt_rand(100000,999999); 
echo $random;
$sql ="SELECT FROM table where ordernumber=$random";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
// regenerate random number 
}
else{
//insert here
}
?>

but this is not good approach rather use auto increment

Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
-1

Before inserting check whether the number exist in databse. If yes generate another random number and do the checking again and so on.But i recomend you to use the auto increment feature to do the same

웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • This is a bad idea because the number might be inserted right between checking if it exists and the insert. If you want to take this approach it *must* be done by setting a constraint in your database. – Jasper Sep 27 '18 at 10:11