I want to write a php a function which will create 50000 unique random alpha numeric string of length 4 and insert it into a db table. how can I do it?
3 Answers
for($i = 0; $i < 50000; $i++)
$pdo->exec("INSERT INTO table_x (the_string) VALUES (UUID());");
As documented here
Edit: alternative insert manner (striping the dashes)
for($i = 0; $i < 50000; $i++)
$pdo->exec("INSERT INTO table_x (the_string) VALUES (REPLACE(UUID(), '-', ''));");
Edit worth mentioning:
Within one single server the UUIDs encode geographic location and precision time, along with sha-1 random values.
Thus the probability of collision only exists within separate servers (for example when merging their data sets).
So long as we don't overflow the capacity of the geo/time slots it is guaranteed to not create duplicate values locally.
As a matter of optimisation (speed of database reads) casting the UUID to a binary(16) field (and having the column in the table to match that datatype) is faster and more compact.
13 Comments
UUID() function being used inserts a string with -. The OP required a alpha numeric string - no where did he/she mention special chars like hyphens. -1This function will generate an array of alphanumeric codes, using whatever character set you like, and whatever length you require. It generates no sequential duplicate letters.
function GenCodes($howmany=50000) {
$charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$cl = strlen($charset);
$codelength = 4;
$result = array();
$code = array();
$lastchar = "";
for ($x = 1; $x <=$howmany; $x++) {
for ($i=1; $i<=$codelength; $i++) {
while(($l = rand(1,$cl-1)) == $lastchar)
;
$code[$i] = $charset[$l];
$lastchar = $l;
}
$code = implode($code);
$result[$x] = $code;
$code = "";
$lastchar = "";
}
return $result;
}
And this one will save them to a database table ensuring there are no duplicates.
function SaveCodes($codes) {
global $dbHost, $dbPort, $dbUser, $dbPass, $dbName;
$insctr = 0;
$db = new db($dbHost, $dbPort, $dbUser, $dbPass, $dbName);
foreach($codes as $code) {
$sql = "select code_id from codes where code_code='".$code."'";
$result = $db->Query($sql);
if ($db->NumRows() == 0) { // don't generate an in-use code
$sql = "insert into codes (code_code) values ('".$code."')";
$result = $db->Query($sql);
if ($result) {
$insctr++;
}
}
}
}
7 Comments
Use a string representation of the 100 number system. Digits would be 0, 1, ..., A, ..., Z, ..., a, ..., z These are only 62 digits so you should use other 38 alphanumeric characters, such as Á á É é Ó ó Ö ö Ő ő Ú ú Ű ű and so on. Each four character string should be part of your set of 100 possible digits. You start from 0000 and add randomly a number to this which would be at least 1 and maximum 2000 in number system 10. The result of the addition (represented as a string with the abstract meaning of a number in the 100 number system) will be your first number. From the second number on all your numbers should be generated by adding the same random offset (between 1 and 2000) to the last generated number. This way your strings will be unique.
Also you should generate batches for insertion, because it's better to have 50 inserts with 1000 rows to insert each than to have 50000 database requests.