Jump to content

create unique id for mysql?


FraanXT
Go to solution Solved by FraanXT,

Recommended Posts

Hello guys,

 

I have a php page that inserts some data in a table, example: Name, email, password. Every line have an identificator(ID) that have autoincrement, so when I insert some data it generates the following number(exemple if last line have id 13, id 14 will be the next(as always).

 

I want to continue using this method but without using this id type, I want to use random ids like 1234567890 and the next line 4324234234(exemple), but always a new and unique id, pretty simple.

 

Would have to make it creating a random number, checking in table if it exists and then insert it? or I can make it with mysql or any fastest method?

 

Sorry for my english, I don't know how to explain that.

 

If someone can help me please.

Link to comment
Share on other sites

your database table(s) should continue to use the auto-increment id as a primary key. this id should be used for internal purposes, i.e. relating database tables to each other, identifying the user in a login session variable...

 

if you need to have a random and unique id for external use, i.e. a remember-me cookie, a password reset token, a form/link consumable token,..., then generate a value using uniqid(), and store this for each visitor in addition to the auto-increment id.

 

you can enforce unique values using the database table, by defining any database column storing the values as a unique key and checking if when storing the value if the query produced an error for a duplicate value.

Link to comment
Share on other sites

your database table(s) should continue to use the auto-increment id as a primary key. this id should be used for internal purposes, i.e. relating database tables to each other, identifying the user in a login session variable...

 

if you need to have a random and unique id for external use, i.e. a remember-me cookie, a password reset token, a form/link consumable token,..., then generate a value using uniqid(), and store this for each visitor in addition to the auto-increment id.

 

you can enforce unique values using the database table, by defining any database column storing the values as a unique key and checking if when storing the value if the query produced an error for a duplicate value.

How uniqid() works? 

Link to comment
Share on other sites

rand(), mt_rand() and uniqid() produce very poor pseudo-random numbers derived from trivial factors like the server time and the process ID. With a bit of effort, it's in fact possible to predict the next value.

 

If the IDs aren't secret, and if the traffic of your website is low, then those functions may still be good enough in your case. But if you want “real” random numbers, you must use the generator of your operating system. There are three ways to access it:

  • through openssl_random_pseudo_bytes() as mentioned by mac_gyver
  • through mcrypt_create_iv()
  • by reading directly from the system device (e. g. /dev/urandom)

If you read 16 bytes from any of those interfaces, you don't have to worry about collisions. There won't be any.

Edited by Jacques1
Link to comment
Share on other sites

  • Solution

rand(), mt_rand() and uniqid() produce very poor pseudo-random numbers derived from trivial factors like the server time and the process ID. With a bit of effort, it's in fact possible to predict the next value.

 

If the IDs aren't secret, and if the traffic of your website is low, then those functions may still be good enough in your case. But if you want “real” random numbers, you must use the generator of your operating system. There are three ways to access it:

  • through openssl_random_pseudo_bytes() as mentioned by mac_gyver
  • through mcrypt_create_iv()
  • by reading directly from the system device (e. g. /dev/urandom)

If you read 16 bytes from any of those interfaces, you don't have to worry about collisions. There won't be any.

 

Thank you bro, helped me a lot.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.