I am thinking to use $uniqueId = substr(md5(time() * mt_rand()),0,5);
to generate unique IDs for booking reference.
But how about the duplication of this generated ID? what if the database already have the same ID that was generated by this? How many percentage of chance to there will be generating same IDs if I use this?
mt_rand + time()生成密钥的准确性和唯一性如何用作预订参考ID? [关闭]
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dougan0529 2014-09-18 08:29关注
From a human point of view, an md5 wouldn't make a good booking reference - it'd be quite awkward to relay over the phone, for example.
But from a development point of view, you perhaps don't want to use a straight auto-increment, as it could give away information about the size of the business (a low number might suggest very few customers and erode customer confidence).
Options include:
Using an element of the date and something relating to the user to generate a reference. Such as
2014/09/CUSTOMERSURNAME01
Using an element of the date and a random
In all options, a quick call to the DB to check for uniqueness will give you confidence that you'll never create a duplicate. It wouldn't add much overhead to check, and if you get a duplicate try again. The chances of having to try again twice would be very low.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报