Jump to content

Make a uniqe user id number


gtal3x

Recommended Posts

Hello am trying to make a function wich would give a 6 digit uniqe number ex. 123456

The problem i am having is that i dont wont to register more then 1 users with the same id number so am thinking of making a database check if that id number already exist, if exist retry the rand() function until the id is uniqe. The little code i thot of is this:

function uid($from,$to) {	
$random = rand($from, $to); 
$checkuid = mysql_query("SELECT usrid FROM users WHERE usrid='$random'");
$uid_exist = mysql_num_rows($checkuid);
if($uid_exist == $random) echo "usrid exist";
else echo $random;	
}
uid(100000,999999); 

What i wont it to do now is insted of printing "usrid exist" but to retry the rand() function until the uniqe id found... Thanks in advance!

Link to comment
Share on other sites

u can use a while loop which doesn't end, unless a available id is found

 

<?php

function uid($from,$to) {	
$random = rand($from, $to);
while ($i==0)
{ 
  $checkuid = mysql_query("SELECT usrid FROM users WHERE usrid='$random'");
  $uid_exist = mysql_num_rows($checkuid);
  echo ($uid_exist == 1) ? end : $random; // i changed this
}
}
uid(100000,999999);


?>

 

note, i changed ($uid_exist == 1) , because $uid_exist can be only 1 or 0 , because if same id found, $uid_exist will be 1, else it'll be 0, its the number of rows ...

 

is userid in the mysql table set to auto_increment / unique ? that's a faster and better way to give unique id

Link to comment
Share on other sites

Err..why not just make the field in the database an auto increment, primary key field? Would be much easier.

 

Not only that, but there is of course the possibility of an infinite loop with the way you are doing it at the moment. If you had the majority of your numbers already taken, it could take an awful long time to find a unique one.

Link to comment
Share on other sites

u can use a while loop which doesn't end, unless a available id is found

 

<?php

function uid($from,$to) {	
$random = rand($from, $to);
while ($i==0)
{ 
  $checkuid = mysql_query("SELECT usrid FROM users WHERE usrid='$random'");
  $uid_exist = mysql_num_rows($checkuid);
  echo ($uid_exist == 1) ? end : $random; // i changed this
}
}
uid(100000,999999);


?>

 

note, i changed ($uid_exist == 1) , because $uid_exist can be only 1 or 0 , because if same id found, $uid_exist will be 1, else it'll be 0, its the number of rows ...

 

is userid in the mysql table set to auto_increment / unique ? that's a faster and better way to give unique id

the code doesnt work for some reason, the browser freezes and after 30 seconds i get something like that :

82320282320282320282320282320282320282320282............................

any solution to that?

Link to comment
Share on other sites

function uid($from,$to) {	
$random = rand($from, $to);
while ($i==0)
{ 
  $checkuid = mysql_query("SELECT usrid FROM users WHERE usrid='$random'");
  $uid_exist = mysql_num_rows($checkuid);
  if ($uid_exist == 0) {
     $i=1;
      return $random;}
}
}
{/code]

That would be what I'd do...

Link to comment
Share on other sites

I'd follow GingerRobots advice, setting the auto_inc to start at 100000.

 

However if you really must have a random solution, then a slight change to Tidalik's code

 

<?php
function uid($from,$to) {	
    $random = rand($from, $to);
    while (1)
    { 
        $checkuid = mysql_query("SELECT usrid FROM users WHERE usrid='$random'");
        $uid_exist = mysql_num_rows($checkuid);
        if ($uid_exist == 0) 
        {
            return $random;
        }
        else $random = rand($from, $to);
    }
}
?>

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.