Check DB, then create random number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Check DB, then create random number

Post by donny »

Hello,

can somebody please help me create a random number but it can't exist already in my database.

thank you
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Check DB, then create random number

Post by Celauran »

You'll need to be more specific. Start by looking at rand().
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Check DB, then create random number

Post by donny »

this is what i am working with now

Code: Select all

<?php 
$random = mt_rand(10000,99999); 
echo $random;
$sql ="SELECT FROM orders where ordernumber=$random";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
// regenerate random number 
}
else{
//insert here
}
?>
can somebody please help me add code to regenerate number if it exists in the db and if the new random number also exists regenerate another one i want to repeat it 5 times just incase. then when its finally made one that doesn't exist i would like the random number to be a variable $ordernumber

thank you very much!!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Check DB, then create random number

Post by Celauran »

Rather than generating a random number, hitting the database to see if it exists, generating another, hitting the database again, could you not use something like UUIDs, which are almost guaranteed to be unique? If not, I'd pull down all values for order number in a single query, store it as an array, and compare your generated randoms against that array.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Check DB, then create random number

Post by donny »

i looked into uuids but theyre pretty long. i just need a 5 digit random number for each order
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Check DB, then create random number

Post by requinix »

What are you going to do for the 100,000th order?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Check DB, then create random number

Post by Celauran »

UUIDs are 36 characters, so maybe that's too long for an order number, but surely 5 is too short. What if you used something like a substring of the hash of the current time? Not random as such, but less prone to collisions and a far greater number.

Code: Select all

hexdec(substr(sha1(microtime()), 0, 10));
or something along those lines?
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: Check DB, then create random number

Post by donny »

that will work fine. thank you very much
Post Reply