spaceman12 Posted December 22, 2010 Share Posted December 22, 2010 Hey guys, I m not yet an experienced coder. SO faced alot of problems. Here, I'm trying to generate a UNIQUE RANDOM NUMBER set between two numbers. Repetative occurance must be avoided by all means as I want every number so generated bears a unique value. In other words, every values that made entry into the field of my database should be differant from each others. <?php $conN=mysql_connect("localhost","root",""); if(!$conN) { die('error'.mysql_error()); } mysql_select_db("freebie_allusers",$conN); $UIN=mt_rand(1,5); $locateUIN="SELECT UIN FROM user_info WHERE UIN='".$UIN."'"; $fetchUIN=mysql_query($locateUIN); $resultUIN=mysql_num_rows($fetchUIN); if($resultUIN>0) { WHAT CODE IS REQUIRED HERE SO AS TO GENERATE UNIQUE RANDOM NUMBER? } else echo $UIN; ?> Thanx in advance Quote Link to comment https://forums.phpfreaks.com/topic/222421-php-generating-unique-number-for-database-field/ Share on other sites More sharing options...
msaz87 Posted December 22, 2010 Share Posted December 22, 2010 You can use auto increment for a key column in the table and every row will automatically increase -- therefore it'll always be unique. If you want to generate one, you already used mt_rand and that and rand are probably the way to go -- unless I'm not quite understanding what you're trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/222421-php-generating-unique-number-for-database-field/#findComment-1150477 Share on other sites More sharing options...
jaikob Posted December 22, 2010 Share Posted December 22, 2010 use rand(). <?php $random_number = rand(); ?> To be specific about the number length, read the manual entry for rand on php.net. What a lot of people do is create an id field and set it as a primary key with auto increment enabled. That way you don't even have to mess with the id. But you have stated you don't want repetition. The reason a lot of others do this is because with rand you always have the slight possibility of trying to insert a number you have already used. Especially if your using a primary key. Quote Link to comment https://forums.phpfreaks.com/topic/222421-php-generating-unique-number-for-database-field/#findComment-1150479 Share on other sites More sharing options...
spaceman12 Posted December 22, 2010 Author Share Posted December 22, 2010 Those generated numbers will serve as a primary Identification code for all users and it should be auto generated by the server itslef. All the number so generated will bear a modulo of some number which i can set it later on but what i'm trying to do at this stage is to abstain the events of recording the same value of number in the database field. Quote Link to comment https://forums.phpfreaks.com/topic/222421-php-generating-unique-number-for-database-field/#findComment-1150483 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.