I want to create a random string about 5 characters long. I can create it normally, but I have problems checking if it exists in the array (or in a real database) and when creating a new one.
I use a function like this to generate a string:
function rand_string(){ return substr(md5(microtime()), 0, 5); }
But I lost them.
Try the following:
function rand_string(){ $str = substr(md5(microtime()), 0, 5); if(exists_in_db($str)) $str = rand_string(); return $str; }
, , , , , .
, , , , , ...
, , , , , . , , , , . .
, : P
, .
$unique=FALSE; while(!$unique) { $str = substr(md5(microtime()), 0, 5); //Insert SQL Code to check if used here if($row['ID']=='') $unique=TRUE; }
in a database that you could:
select substr(newid(),1,5) as name
for a new line and for checking ..:
select count(*) as cnt from yourtable where yourcolumn = __GENERATED_string__
create time around him and you will do
Using mysql_query (), use the select statement to see if you returned any results (ie, “Select * from the table, where col1 =" String ". Then check to see if the rows were returned. Until you have truly random, unused value.
Source: https://habr.com/ru/post/1758435/More articles:ftp to F # - f #PHP Does try / catch have higher overhead than if? - optimizationHow to find an unused port? - .netHow to calculate max. is the number of combinations possible? - algorithmShowing possible string combinations - phpCombining a PHP array with order from left to right - arraysКак POST-образные данные изображения из java - javaDo assistants help faster than partial ones? How about the structure of the strings? - performanceSitecore: permission to publish user / role - publishingLINQ query optimization? - linqAll Articles