Nandini Posted November 11, 2011 Share Posted November 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/ Share on other sites More sharing options...
RobertP Posted November 11, 2011 Share Posted November 11, 2011 should not loop more then once or twice. while(true){ $id = generateNewId(); $res = mysql_query("select id from members where id='{$id}';",$con); if(mysql_num_rows($res)==0) break; } Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287287 Share on other sites More sharing options...
Nandini Posted November 11, 2011 Author Share Posted November 11, 2011 Thanq Robert. If we should not loop more than once, how can i solve this issue. Please let me know. Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287289 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287294 Share on other sites More sharing options...
Nandini Posted November 11, 2011 Author Share Posted November 11, 2011 Thanx. Is this correct design? Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287305 Share on other sites More sharing options...
cypher86 Posted November 11, 2011 Share Posted November 11, 2011 if it works for you .... yes Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287310 Share on other sites More sharing options...
The Little Guy Posted November 11, 2011 Share Posted November 11, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/250920-generating-php-random-user-id-and-save-into-mysql/#findComment-1287366 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.