PHP: how to generate a unique digit ID. Note that it is a number. Do not use a string. PHP: how to generate a unique digit ID. Note that it is a number. Do not use a string.
Reply content:
How to generate a unique numeric ID in PHP? Be sure to use numbers instead of strings.
You have a big problem.
Twitter, weibo, etc. are all dedicated to a sender to solve this problem.
The twitter set is called snowflake, which has been pointed out upstairs. This is a total of 64bit, the first 41bit is a subtle timestamp, 10 bit is the server id, and the last 12bit is the seq sequence accumulation counter.
The weibo method is similar to twitter. It divides IDS into n segments, collects data sources for each segment, and generates a highly unique id.
Flickr is a tool called ticketserver, which is implemented using mysql only.
Create table ticket (
Id bigint unsigned not null auto_increment primary key,
Stag char (1) not null default '',
Unique keystag(stag)
) Engine = myisam;
Insert a record, and then use replace to obtain the id.
Replace into ticket (stag) values ('A ');
Select last_insert_id () ;==> ID
Then, UUID is also a good choice, but the UUID generation sequence is too long, and mysql itself does not have native support (I pretend that the landlord uses mysql ), however, it would be better for the landlord to try to save the uuid as binary (16. But the landlord said it was a pure number, and that would be nothing.
Another one is what I heard in the group. It is to generate a large batch of available unique IDs in advance, and take and del them directly when using them. This is also feasible because the person who said this solution is a great god. Why is it generated in advance? I think the most important one is to prevent the two people from getting the same id in the case of high concurrency. If they are generated in advance, they can be generated one by one to ensure that they are unique. Of course, I don't know what their generation policy is. The landlord can refer to twitter's practices.
In addition, mongodb's built-in objectId is also a highly unique sequence. The landlord can use the objectId generated by Mongodb to directly use it. However, in the case of high concurrency, two or more people get the same id, although the probability is very low. However, the landlord said it was a pure number, and that was nothing.
If you are playing with a single host, you don't have to worry too much about the unique id problem. The primary key auto-increment can be used as a unique id. The problem is too long. You can consider it now, but you cannot be too rigid. If the landlord is distributed, it is necessary.
base_convert(uniqid(), 16, 10);
Yes
redis incr
We recommend a unique digital ID generator that I am using.
Https://github.com/sschiau/Particle.php
Participant lelanguage: PHP64bits int Time Based ID Generator
PHP implementation of Twitter Snowflake ID Generator
Uncoordinated
For high availability within and other SS data centers, machines generating ids shoshould not have to coordinate with each other.
Solution
System Clock Dependency
You shoshould use NTP to keep your system clock accurate.
How to use itGenerate Particle ID
Change const EPOCH in particle class to today epoch time w/miliseconds (13 digits)
$machineID = 1; // Machine ID (aka Server ID no) Particle::generateParticle($machineID);
Time from Particle ID (w/milisecond precision)
$particleID = '4611692470816737853'; Particle::timeFromParticle($particleID);
The owner should describe the unique ID Usage and use cases. The database auto-incremental ID, and PHP are weak types. Numbers and string numeric types can be common and converted to each other.
No way, unless you record the generated numbers, even if the random function generates a number with a timestamp, it will be repeated.
Twitter globally unique ID Generation Service: snowflake (https://github.com/twitter/snowflake)
Reference:
Http://www.oschina.net/p/snowflake
Http://www.cppblog.com/tx7do/archive/2014/06/10/207248.html
Uniqid is really good, why don't you use it? Just need convert to number, first answer is cool.
Sorry I can't type Chinese, because it's disabled on chrome, I think it's a bug of segmentfault.com
First, we should explain how often a single machine is unique, and whether a single request is unique or multiple requests are unique.
Timestamp and random number combination, but it is best to verify it in the recorded ID pool after it is generated.
@ Magicgod I think you can @ SegmentFault to solve your question
I don't quite understand what the subject means. What scenarios are used in? Can I set ID auto-increment directly in the database? If it is an array, then $ I ++
Timestamp
You can generate a uuid or guid that ensures that there are no duplicates in the same time and space. There are many open-source classes that you can write by yourself, according to the current timestamp + random factor. the random factor can include the mac address of the current pc, the random string, and the number of lines of the current Code. PHP built-in functions:
Com_create_guidYou can complete your functions. If you are not satisfied, there are many open-source implementations on github.
Supplement: 1 should be fixed. 2. Convert letters into numbers or randomly generate numbers.
There is a way to use the current timestamp, pure number
Paste a piece of pseudo code from me and the id generator I used
$id = substr(strtotime(date("Y-m-d", time())), 0, 2) . substr(strrev(microtime()), 0, 2) . substr(mt_rand(), 0, 5) . substr(rand(), 0, 2);$sql = "SELECT id FROM table WHERE id=" . $id . " LIMIT 1";$data mysql_query($sql);
The idea is probably to splice the timestamp with a random number. The specific implementation will be modified based on the requirements of the landlord.
Although it is possible to obtain a unique value based on the calculation, it cannot be ensured that it is best to add a unique key to the database and ensure the unique value through the query.
Generate a number pool in advance and retrieve it from the pool when using it.
If the number is released, simply put it back into the pool.
In this case, random information can be effectively solved.