Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Unique String in PHP for Mysql Database

Tags:

php

mysql

How can I generate a unique Key and insert it into my mysql database?
This is what I currently use:

echo uniqid(time(), true);

Can you tell me wether it is a unique key or not?

like image 279
Yaser Ranjha Avatar asked Oct 27 '25 08:10

Yaser Ranjha


2 Answers

You can use AUTOINCREMENT on your id field in the database.
It will never get duplicate since it increments the highest existing id present.

you can use following to get unique id with 32 chars

md5(uniqid(time(), true))

or you can use sha1 like with 40 chars

sha1(uniqid(time(), true))
like image 31
Harish Singh Avatar answered Oct 28 '25 23:10

Harish Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!