mforan Posted October 23, 2008 Share Posted October 23, 2008 how would i loop this to ensure that "erg34t3t4errgg" for example, would not appear twice? <?php # UNIQUE KEY $letters = range('a', 'z'); shuffle($letters); $uniqueid = md5(time() . implode('', $letters) . rand()); # ADD KEY TO DATABASE $query = "INSERT INTO attacks SET id ='$uniqueid',username='$user',attacked='$buildchange[1]'"; mysql_query($query) or die("Error: ".mysql_error()); ?> i know the chances are slim, that it would happen. would i use a for function? Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/ Share on other sites More sharing options...
limitphp Posted October 23, 2008 Share Posted October 23, 2008 If you are trying to create a uniqueID that will never appear twice in a database, I don't think you need to use loops. Its taxing resources unnecessarily. Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672929 Share on other sites More sharing options...
wildteen88 Posted October 23, 2008 Share Posted October 23, 2008 Set your id field in your accounts table to Unique. Now MySQL will prevent duplicate entries of the same unique id being inserted into the id column. Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672932 Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 if you are looking for a unique field, why not just use an auto_increment? Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672936 Share on other sites More sharing options...
limitphp Posted October 23, 2008 Share Posted October 23, 2008 Set your id field in your accounts table to Unique. Now MySQL will prevent duplicate entries of the same unique id being inserted into the id column. So, if I have a table with an ID field. And I don't want the ID entries to ever be repeated, could I just use the uniqueID function and set the field to Unique? And then, if an entry is ever the same as another one, it will change it automatically for me? Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672939 Share on other sites More sharing options...
mforan Posted October 23, 2008 Author Share Posted October 23, 2008 If you are trying to create a uniqueID that will never appear twice in a database, I don't think you need to use loops. Its taxing resources unnecessarily. yer i know what you mean, id use auto increment. trouble is, the way the code is layed out i will have to write the id number, then write the information later. unless there is a way to pull off the last entry of say username='username' in the table? Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672944 Share on other sites More sharing options...
rhodesa Posted October 23, 2008 Share Posted October 23, 2008 insert the record with auto_increment, then get the ID that was automatically generated with mysql_last_insert_id() Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672952 Share on other sites More sharing options...
discomatt Posted October 23, 2008 Share Posted October 23, 2008 PHP's uniqid() or MySQL's UUID() might help with this as well. Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672959 Share on other sites More sharing options...
mforan Posted October 23, 2008 Author Share Posted October 23, 2008 $uniqueid = mysql_insert_id(); lol, works a treat, shall have to remember this! Link to comment https://forums.phpfreaks.com/topic/129809-loop-to-add-generated-key/#findComment-672960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.