Create a serial or unique order number

How to create unique serial number or order number in php without checking on db?

+4
source share
4 answers

see uniqid or uuid_create in uuid pecl or com_create_guid ()

+5
source

If you want to guarantee uniqueness, you need to check something. You can use a flat file and store only the last used number (which you can increase), but it would be foolish not to use a database. You need to save a bunch of other information related to the number (contact details, order, etc.), and the database is ideal for this.

+1
source

In mysql, there are several problems with using auto-increment columns (not least the fact that it does not scale for equivalent nodes).

Although you can support the sequence generator almost anywhere (memcache, files, database), PHP does not use the extended semantics of file locking - in addition to the performance impact, you can quickly find yourself in a transaction blocking situation. It does not scale to large volumes.

If you have PL / SQL available, I would recommend using a sequence generator there - as an alternative, you might consider creating a generator in PHP and sqlite.

I would highly recommend implementing your generator to create format numbers:

$ use_number = ++ $ sequence_number. str_pad ($ node_id, '0', $ max_nodes, STR_PAD_LEFT);

Where node_id is a number that uniquely references the storage substrate where the current sequence number is stored. And $ max_nodes is slightly larger than the maximum number of digits found in $ node_id (for example, 3 will allow up to 999 nodes).

Alternatively, consider it as a string with punctuation between two parts (and / or store 2 parts in different database columns).

FROM.

+1
source

If you are using a MySQL database, you can create an order serial number without first checking the database using the auto_increment column. Inserting a new row will result in assigning a new unique identifier to this column without having to read / write this column in your PHP.

Alternatively, if you want a globally unique identifier without any database, you can use the PHP uniqid () function. Note that this returns a string. If you are using more than one physical machine or moving between servers, you must add a prefix (first argument) that is different on each machine. When used on the same computer, this function should never return the same string value twice.

0
source

Source: https://habr.com/ru/post/1313352/


All Articles