Jump to content


Photo

Generate a random number & check DB to see it is unique


  • Please log in to reply
4 replies to this topic

#1 playaz

playaz
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 08 March 2006 - 04:13 PM

Hi guys,

I have a registration form that once submitted inserts the values into a mysql db. Its a registration form for a trade account (eCommerce site) - basically once a company applies for a trade account, i'd like an Account Number to be generated automatically however obviously this needs to be unique.

So ideally i'd would like it to randomly generate an 8 digit number and then check the database to ensure it isn't already in use.

Can anyone assist me on this thanks :)

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 08 March 2006 - 05:07 PM

you could loop random number generation checked against the database.

$loop=true;
while($loop) {
  $acct_num = rand(10000000,99999999);
  if(!mysql_query("SELECT * FROM `table` WHERE `account_number` = '".$acct_num."' LIMIT 1")) $loop=false;
} // when found, loop ends, and $acct_num retains a value you can use later.

Some mysql expert should have a better mysql solution -- which would be much faster -- but the above code still works.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 March 2006 - 05:39 PM

I would get all of the current id numbers in the database, store them to a temporary array, then generate the random numbers until you find one not in the temporary array.

Something like:
<?php
$q = "select acct_num from your_table";
$rs = mysql_query($q);
$tmp = array();
while ($rw = mysql_fetch_assoc($rs))
      $tmp[] = $rw['acct_num'];
$new_acct_num = rand(10000000,99999999);
while (in_array($new_acct_num,$tmp)) $new_acct_num = rand(10000000,99999999);
echo $new_acct_num;
?>

There is a slim possibility that this code will produce a duplicate number if more than one new account is processed at the same time (before the database is updated with the new number).

Is there any reason why you can't use an autoincrement field?

Ken

#4 YAOMK

YAOMK
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 01 July 2007 - 12:43 AM

There is a slim possibility that this code will produce a duplicate number if more than one new account is processed at the same time (before the database is updated with the new number).


This can be solved using inodb tables along with transactions.

#5 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 01 July 2007 - 12:48 AM

I would do something like:

$rand = rand(1000000000,9999999999);
while(mysql_num_rows(mysql_query("SELECT number FROM somewhere WHERE number = '{$rand}'")) > 0) {
$rand = rand(1000000000,9999999999);
}

Why doesn't anyone ever say hi, hey, or whad up world?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users