Prashant
Prashant

Reputation: 9

Generating unique order number but not want to use auto incremented

How can can generate unique order number. I do not want auto incremented using data base. i am using the below code

$check_oid_from_cart_query = "
    SELECT 
        MAX(CONVERT(orderid, UNSIGNED INTEGER)) AS orderid 
    FROM  tbl_cart_hist 
    WHERE locationid = '$lid' 
    AND   order_type <> '5'";
$check_oid_from_cart_result = dbQuery($check_oid_from_cart_query);

when i used this and get two request at the same time ,I am getting the same order number .

Upvotes: 0

Views: 149

Answers (1)

Peter-John Morgenrood
Peter-John Morgenrood

Reputation: 11

this question has been answered here using various methods some of which are referenced at [http://phpgoogle.blogspot.co.za/2007/08/four-ways-to-generate-unique-id-by-php.html][1] but seeing that question was asked 3 days ago, I assume you are looking for something simple, maybe the following might help?

Option 1: Use time represented in microseconds. microtime() returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call.

$microseconds = round(microtime(true));
$orderid = $microseconds;
echo $orderid;

Option 2: I just came up with this technique so not sure if it is 100% production ready yet. Try Append day of year, last 2 digits of year, current time in minutes, current time in seconds & random 1 digit number (Depending on daily client base you can increase (mt_rand(1,100)) )

//mt_rand for random digit 
$random = (mt_rand(1,9));
// date('z') gives the day of year i.e today is day 106 of the year 2017
$date = date('zyis'); 
$date .= $random;
$orderid = $date;
echo($orderid); 

Output would look like 1051738225

Play with the idea and let me know if you found a better method.

Upvotes: 1

Related Questions