-3

Here is one for those well versed in PHP as well as MYSQL.

I'm looking for a way to generate unique but yet random long numbered IDs for each entry into the database.

I don't want it to start with the usual one,

I'd like something a bit longer. Does anyone know of any simple function or code snippet that would achieve this?

2
  • 1
    Algorithm for generating a random number Commented Apr 30, 2013 at 10:26
  • 1
    You have no idea how many problems you imposed just by asking that question. The very fact you want to do that from PHP completely breaks the consistency. Commented Apr 30, 2013 at 10:28

5 Answers 5

3

You could change the AUTO_INCREMENT seed for the PRIMARY KEY of your table to get an initially greater number.

CREATE TABLE example (
  nid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50)
) AUTO_INCREMENT = 200000;

The next value generated for nid when inserting a row into your table will be 200001

To change the AUTO_INCREMENT seed for an existing table you could use:

ALTER TABLE example AUTO_INCREMENT = 200000;
Sign up to request clarification or add additional context in comments.

Comments

2

use function UUID();

you can read more on

http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

mysql> SELECT UUID();
    -> '6ccd780c-baba-1026-9564-0040f4311e29'

A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format

for random number use RAND()

SELECT (FLOOR( 1 + RAND( ) *10000 ))

Comments

0

Use random.org to generate your random numbers

Its almost impossible in php to generate true random numbers. Over a large enough sample the flaws are obvious as PHP uses pseudo-random number generator for things like rand() etc

http://www.random.org/randomness/

You can generate and call your random number using curl.

Comments

0

Is it on insert, or to update an existing table?

Assuming for an insert you could use something like this.

INSERT INTO someTable (aField, aNotherField, SomeNumber)
SELECT 'aFieldValue', 'aNotherField', aNum
FROM (SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum
UNION SELECT ROUND(RAND() * 899999) + 100000 AS aNum) Sub1
LEFT JOIN someTable ON Sub1.aNum = someTable.SomeNumber
WHERE someTable.SomeNumber IS NULL
LIMIT 1

This is generating 10 random numbers between 100000 and 999999, and picking one that hasn't already been used.

It is a pretty nasty way of doing this, and has the obvious major flaw that all 10 numbers generated could have already been used.

I would be inclined to suggest that the need for a random number can likely be avoided.

Comments

-1

This is as unique as it can be:

$uniqueValue = uniqid('', true)

2 Comments

I'm not anymore ... :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.