unicoart Posted October 27, 2011 Share Posted October 27, 2011 Hi, i need to populate a mysql database with all alphanumerical combinations with length of 5(letters/numbers) So: $mix = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9)); 61^5 , i guess that would be 844 596 301 rows in table. can anybody help me with this snippet? Quote Link to comment Share on other sites More sharing options...
unicoart Posted October 27, 2011 Author Share Posted October 27, 2011 <?php function permutations($letters,$num){ $last = str_repeat($letters{0},$num); $result = array(); while($last != str_repeat(lastchar($letters),$num)){ $result[] = $last; $last = char_add($letters,$last,$num-1); } $result[] = $last; return $result; } function char_add($digits,$string,$char){ if($string{$char} <> lastchar($digits)){ $string{$char} = $digits{strpos($digits,$string{$char})+1}; return $string; }else{ $string = changeall($string,$digits{0},$char); return char_add($digits,$string,$char-1); } } function lastchar($string){ return $string{strlen($string)-1}; } function changeall($string,$char,$start = 0,$end = 0){ if($end == 0) $end = strlen($string)-1; for($i=$start;$i<=$end;$i++){ $string{$i} = $char; } return $string; } /* ************************************************* */ $mix = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9)); $mix = implode("", $mix); $Array=permutations($mix,2); for($i=0 ; $i < count($Array) ; $i++) { echo "$i." . $Array[$i] . "<BR>"; } ?> i think this will work Quote Link to comment Share on other sites More sharing options...
unicoart Posted October 27, 2011 Author Share Posted October 27, 2011 noup :'( , too many RAM memory consumption, i am open to ideas..... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 This would work: require_once "incl/db_connections.php"; $rows = 1000; function genCode($len = 5){ return substr(md5(rand(0,1000).time().microtime().rand(0,1000)), 0, $len); } for($i=0;$i<$rows;$i++){ $randCode = genCode(); mysql_query("insert into my_table (code) values ('$randCode')"); } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2011 Share Posted October 27, 2011 For what purpose are you doing this. There is most definitely a better solution than populating the database with almost a billion entries. Quote Link to comment 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.