0

I was wondering how to generate a random 9 digit number with php after a html form submit.

in english:

I have a form on my site....which posts data to a database. I would like to add another function to the php file that will generate a number for every form submit (something like a unique id number for every user that submits the form)

I can do all the database stuff. Just need to know how to generate the number.

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • If you want full control over the digits and length: http://stackoverflow.com/questions/853813/how-to-create-a-random-string-using-php – Eric J. Jul 18 '11 at 15:07
  • 1
    Instead of a random ID use an Autoincrementing Primary Key – Gordon Jul 18 '11 at 15:13

6 Answers6

2
rand() = Max 32768 on windows machines

so if you choose to use:

mt_rand (100000000,999999999);

but i suggest you use the AI id from the database and pad it if your curtain you need length 9

str_pad(mysql_insert_id(), 9, "0", STR_PAD_LEFT);
000000001
000000002
000000003
...
000000199
and so on
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • so this would generate a unique number each time? (no duplicates) –  Jul 18 '11 at 15:24
  • mt_rand would create dupes from time to time like any – Lawrence Cherone Jul 18 '11 at 15:32
  • ah, see the thing is... i later want to use this number to select certain rows from the table in the database with PHP. If there are duplicates, php would get confused. –  Jul 18 '11 at 15:34
  • well then you should use the PRIMARY KEY like everyone is suggesting. – Lawrence Cherone Jul 18 '11 at 15:35
  • also that PRIMARY kEY can be used to link tables together later on as your project grows, so you can select from multiple tables in one query... – Lawrence Cherone Jul 18 '11 at 15:39
  • hey, I've decided to use the primary key with auto_increent. I just wanted to know, will this generate a completely random number like 624985673 and not something like 123456789? as far as i'm concerned, the auto_increment goes 01, 02, 03, etc etc –  Jul 18 '11 at 16:55
1

Why reinvent the wheel? I recommend you have your database create a unique id via UUID().

Raffael
  • 19,547
  • 15
  • 82
  • 160
  • could i later on select a row from a table in my database with this unique id? –  Jul 18 '11 at 15:26
0

Try the uniqid function.

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

Ryan
  • 26,884
  • 9
  • 56
  • 83
0

have a look at the rand function

PHP manual: Rand()

Fender
  • 3,055
  • 1
  • 17
  • 25
0

you can do database stuff and you cannot generate an id?

random: rand(1, 999999999)

or from that insert i assume mysql: mysql_insert_id() - if you use ID's

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
0

I cannot imagine why you would do this instead of using a PRIMARY KEY with AUTO_INCREMENT, but since you ask:

mt_rand() is the way to do.

DO NOT USE rand() -- IT IS SLOW AND FLAWED

For proof of that concept, try the following:

header("Content-type: image/png");
DEFINE('SQUARE_SIZE', 100);
$img = imagecreatetruecolor(SQUARE_SIZE, SQUARE_SIZE);

$ink = imagecolorallocate($img, 255, 255, 255);

for($i = 0, $max = pow(SQUARE_SIZE, 2); $i < $max; $i++) {
    imagesetpixel($img, mt_rand(1,SQUARE_SIZE), mt_rand(1,SQUARE_SIZE), $ink);
}

imagepng($img);
imagedestroy($img);

You'll get pixel noise; if you change the mt_rand calls to rand, it will be a very obvious repeating pattern, and the same pattern every time.

example is adapted from php manual comments for mt_rand()

Community
  • 1
  • 1
Dereleased
  • 9,939
  • 3
  • 35
  • 51
  • ive tested that before and when the image is larger then 500,500 its not so random, baffling... – Lawrence Cherone Jul 18 '11 at 15:34
  • I don't see that at all. Tried several sizes from 50x50 to 1024x1024 and `rand()` always shows a distinct pattern that repeats OFTEN, and `mt_rand()` is generally noise. – Dereleased Jul 18 '11 at 15:40
  • I take that back -- at small sizes, it is fairly random, not too different from `mt_rand`; however, patterns emerge at 115x115, and any larger and they become strikingly obvious. By 200x200 the image is practically static if generate multiple times. `mt_rand` does not suffer from this. – Dereleased Jul 18 '11 at 15:44
  • hey, I've decided to use the primary key with auto_increent. I just wanted to know, will this generate a completely random number like 624985673 and not something like 123456789? as far as i'm concerned, the auto_increment goes 01, 02, 03, etc etc –  Jul 18 '11 at 16:07