I am wanting to create a unique user id for my
clients, so I am taking the first 3 letters of there
name and adding the 4 digits from the rand() function
If the clients namd is david,
this shoud give me something like:
$Db_user = davi7024
Now I want to check that this IS a unique user id so
I look it up in the database.
Now if the record exists, I want to regenerate the user-id with the rand
again and then re-check. If found, do again and re-check again etc.
Do do the multiple re-checks, I guess that I need to put the
look up in a while loop.
But I am not sure how to use the while loop in this case.
This is what I have so far:
Can someone please help me put this in a while
loop so that I ensure unique user_ids ?
Thanks.
clients, so I am taking the first 3 letters of there
name and adding the 4 digits from the rand() function
If the clients namd is david,
this shoud give me something like:
$Db_user = davi7024
Now I want to check that this IS a unique user id so
I look it up in the database.
Now if the record exists, I want to regenerate the user-id with the rand
again and then re-check. If found, do again and re-check again etc.
Do do the multiple re-checks, I guess that I need to put the
look up in a while loop.
But I am not sure how to use the while loop in this case.
This is what I have so far:
Code:
$rand = rand(124,987); $Db_user = substr($Db_name,0,4).$rand; // Test for unique new contact name. $sql = "SELECT * FROM clients WHERE user_id = '$Db_user'"; $result = mysql_query($sql) or die("could not execute FIND MEMBER query"); if(mysql_num_rows($result) > 0){ $rand = rand(124,987); $Db_user = substr($Db_name,0,4).$rand; exit(); } // endif
loop so that I ensure unique user_ids ?
Thanks.
Comment