Unique numerical ID for Order Number, Invoice, table index or other usage

We use numerical identification for table's primary key, unique key or index key. Unique Key also needed for Invoice number, shopping order number and so on.

In general field type Auto Increment is good for these cases. What if you don't like it or you get requirement for different formatted identifiers.
Do not worry; I have solution and code for this. I got this idea from IGNOU NO and used this idea first time in Laughter Yoga shopping cart.

$sql = "SELECT invoice_no FROM ... ORDER BY invoice_no DESC LIMIT 1";
$res = mysql_query($sql);
$invoiceNo = mysql_result($res, 0, 0);
// OR for sample
// Comment above lines if using this for testing
//if (! empty($_SESSION['no']))
// $invoiceNo = $_SESSION['no'];
if (empty($invoiceNo))
$invoiceNo = 20070000000;
$invoiceNo = substr($invoiceNo, 4) + 1;
echo '<br>';
$invoiceNo = '2007' . str_pad($invoiceNo, 7, 0, STR_PAD_LEFT);
$_SESSION['no'] = $invoiceNo;
echo $invoiceNo;
// Use function "Date('Y')" for getting current year.

Prev: Unique Identification (file, folder, Number)

Comments are open for an year period. Please, write here on Facebook page.