Hi, I wrote the below function to generate an 10 digit order id. When done it's checks the db if there's already one with this number. Is this a good way or does anybody think I can better do somthing else? I'm not sure because when the order tables are filles with lots of records doesn't it take very long??

<?PHP
function GenerateNumber() {
ConnectDB();
srand(microtime()*1000000);
while(strlen($number) < 10) {
$number .= rand();
}
$number = substr($number, 0, 10);
$sql = "SELECT ord.orderid as ID, old.orderid as OldID FROM orders ord, old_orders old WHERE ord.orderid='$number' OR old.orderid='$number'";
if(mysql_num_rows(mysql_query($sql)))
GenerateNumber();
return($number);
}
?>

Kind Regards,
Rik

    • [deleted]

    Normal practise is to let the database create a new number automatically. In MySQL you can define the orderid column to be "auto_increment" which means it will generate a new unique number every time you insert a new record into that table.

      Yes thanks, but I know that but I didn't want the numbers to follow up eachother. There mustn't be any logic between the numbers. That's why I randomize the numbers

      Regards,
      Rik

        It doesnt take that long, unless perhaps your talking about 10's of thousands of records..

          No not so many at all. So thanks for your comment and I continue to use this code.

          Kind Regards,
          Rik

            • [deleted]

            " There mustn't be any logic between the numbers. "

            What's the reason behind that?

              Write a Reply...