Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & mySQL(i): How to generate a random user id?

Tags:

php

mysql

say if I wanted to give every user that registered on my site a unique id. It seems to me that if I wanted to do this I would have to: Create a random number for the id, check to see if that id already exists in the database, if it does exist then create another random number and send yet another query to see if that exists, and so on...

This could go on for ages. Apart from having an incrementing id, is there any decent way to do this?

like image 847
John Smith Avatar asked Jan 05 '12 00:01

John Smith


People also ask

What PHP stand for?

PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym." PHP executes on the server, while a comparable alternative, JavaScript, executes on the client.

Is PHP still used?

PHP is known to be the most frequently used programming language. According to W3Techs, 78.8% of all websites are using PHP for their server-side.


1 Answers

The best way to do this is via the auto increment function, if you really don't want to use a function like so you could use uniqid();

Basically you it generates an unique id based on milliseconds, if you put in a kinda unique prefix in the function it will generate a very unique id.

echo uniqid('prefix');

This way you won't have to check after generating an id, if it already exists or not. You can be sure it is unique.

For more information check this url http://php.net/uniqid!

like image 151
Wesley Avatar answered Oct 03 '22 18:10

Wesley