0

Am trying to use the php mt_rand() function to produce for me a unique number to be used as a unique identifier for a row in the database. Can it pick a number it had previously picked from the range 1, 1000000. My code is as follows echo mt_rand(1, 1000000);?> Thanks for your continued help

2
  • yes it can. what is wrong with mysql primary key as auto-increment? Commented Jul 27, 2015 at 13:38
  • I definitely wouldn't recommend using any kind of rand( ) to mimic a serialization of a database index. auto-increment is the way to go Commented Jul 27, 2015 at 13:41

1 Answer 1

4

Yes, it will produce duplicates. It's just random, not unique. The easiest solution is to use an auto-increment column. If you don't want it to be sequential, you have a couple options. These are the two I would look at first:

https://github.com/ramsey/uuid

http://php.net/manual/en/function.uniqid.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.