gtal3x Posted October 16, 2007 Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/ Share on other sites More sharing options...
chanchelkumar Posted October 16, 2007 Share Posted October 16, 2007 After to your check just call that function for random untill your data insertred in to database... try dis one :mysql_num_rows() Link to comment https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370444 Share on other sites More sharing options...
jd2007 Posted October 16, 2007 Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370449 Share on other sites More sharing options...
GingerRobot Posted October 16, 2007 Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370459 Share on other sites More sharing options...
gtal3x Posted October 16, 2007 Author Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370461 Share on other sites More sharing options...
tidalik Posted October 16, 2007 Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370477 Share on other sites More sharing options...
jd2007 Posted October 16, 2007 Share Posted October 16, 2007 i was just showing how the loop works in that code...u wouldn't wanna use it without modiifying it Link to comment https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370486 Share on other sites More sharing options...
Barand Posted October 16, 2007 Share Posted October 16, 2007 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 https://forums.phpfreaks.com/topic/73428-make-a-uniqe-user-id-number/#findComment-370546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.