Jump to content

Generating PHP random user id and save into MySql


Nandini

Recommended Posts

Hi

 

I am using php5 and Mysql

 

I want to generate user id like phone format (Ex: xxx-xxx-xxxx). and check that user id exists in database or not. if yes, i have to generate new user id etc. If that user id not in database, i have to insert user id for new user. Means i have generate unique user id for login purpose.

 

Generating user id is working fine. availability of user id also ok for me by using mysql_num_rows() function. But if user id i exists in database, how can i generate new user id. All this will be done in single instance (When user click on submit button from registration form).

 

Here is my script.

 

<?php
function UserID()
{
$starting_digit=5;
$first_part1=rand(0,99);
if(strlen($first_part1)==1) { $first_part="0".$first_part1; } else { $first_part=$first_part1; }
$second_part1=rand(1,999);
if(strlen($second_part1)==1) { $second_part="00".$second_part1; } elseif(strlen($second_part1)==2) { $second_part="0".$second_part1; } else { $second_part=$second_part1; }
$third_part1=rand(1,9999);
if(strlen($third_part1)==1) { $third_part="000".$third_part1; } elseif(strlen($third_part1)==2) { $third_part="00".$third_part1; } elseif(strlen($third_part1)==3) { $third_part="0".$third_part1; } else { $third_part=$third_part1; }
$userid=$starting_digit.$first_part."-".$second_part."-".$third_part;
return $userid;
}

$random_userid=UserID();

$sql=mysql_query("select * from users where user_id='".$random_userid."'");
if(mysql_num_rows($sql)==0)
{
$insert=mysql_query("insert into users (user_id) values ('".$random_userid."')");
}
?>

 

If user id not exists in database, that user id inserting in database successfully. If not, empty value is inserting, not generating new user id.

 

Can any one help me out please.

Link to comment
Share on other sites

something like that ??

 

<?php
$random_userid=UserID();

$sql=mysql_query("select * from users where user_id='".$random_userid."'");

while(mysql_num_rows($sql)!=0)
{
$random_userid=UserID();
$sql=mysql_query("select * from users where user_id='".$random_userid."'");
}

$insert=mysql_query("insert into users (user_id) values ('".$random_userid."')");
?>

 

in this way you will loop until you find a user who's not in your db.

Link to comment
Share on other sites

First of all, this id should NOT be a user id, it should be like a username. Mysql has a built in auto increment feature that should be used to to create unique user id's. What you have should be used to create something like a username or user code. You should not be using this to create distinct member ids, because it is bad practice.

 

Take YouTube for example they use a unique string to define a video, but that string is not the actual way they uniquely identify the video through out the database, and if they did the site would be slow because matching on strings is much slower than matching on unique ids.

 

 

So, to create your user's code, could do this:

 

A. Make a id column, which is a primary key and auto_increment

B. Make a column for the members code (xxx-xxx-xxxx) and make it a Unique column

 

do{
$str = round(rand(100,999))."-".round(rand(100,999))."-".round(rand(1000,9999));
$sql = mysql_query("insert into my_table (col1, col2, user_code) values ('$c1', '$c2', '$str')");
$affr = mysql_affected_rows();
if($affr == 1)
	break;
}while(true);

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.