Generate uniqe UID and store to mysql's table | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Generate uniqe UID and store to mysql's table

I have a table that UID column name is its primary key and I use PDO to connect to mysql with PHP. How to generate a unique user id for any new user that has registered and store it(user id) to UID column in my sql? Please help me ☺

10th Mar 2019, 10:06 AM
Hamidreza Abolfathi
Hamidreza Abolfathi - avatar
3 Answers
+ 9
Hamidreza Abolfathi Hey, I agree with Shudarshan Rai 👑 but you can still perform sql select to generate ID. I would use something like this select isnull(max(cast(machines.ID as int)), 0) + 1 from dbo.machines machines /* Idea: New record will always get highest ID from table 1. select ID 2. select max ID to get latest number 3. avoid string value and cast it as intiger 4. handle null values with isnull */
10th Mar 2019, 12:24 PM
r8w9
r8w9 - avatar
+ 4
//Better then generating UID from code storing on database, checking if it's already exist, Do make a column of UID, type=Int and use A.I (auto increment). Whenever new user registers auto gives unique id
10th Mar 2019, 10:26 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
Shudarshan Rai 👑 , r8w9 OK . Thanks for your answers.
10th Mar 2019, 12:43 PM
Hamidreza Abolfathi
Hamidreza Abolfathi - avatar