-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?

hs.chandra
  • 707
  • 1
  • 7
  • 19
Geordie Dave
  • 65
  • 3
  • 8
  • 1
    [Algorithm for generating a random number](http://stackoverflow.com/questions/319524/algorithm-for-generating-a-random-number) – Dom Apr 30 '13 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. – N.B. Apr 30 '13 at 10:28

5 Answers5

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;
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
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 ))
Robert
  • 19,800
  • 5
  • 55
  • 85
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.

Dave
  • 3,280
  • 2
  • 22
  • 40
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.

Kickstart
  • 21,403
  • 2
  • 21
  • 33
-1

This is as unique as it can be:

$uniqueValue = uniqid('', true)
erikvimz
  • 5,256
  • 6
  • 44
  • 60