i have function to generate random uniq code and check this uniq code in table. In this function i use do while if uniq code available in table. But i have some problem, some time i get mysql query to check uniq code in table always running on my SHOW PROCESSLIST in phpmyadmin. would you help me? Trims
function random($db){
$tgl=date('j');
$bulan=date('m');
$front=$tgl;
$rowcount=0;
$loop_on=0;
do{
$randomString = '';
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$characters2 = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characters3 = 'ABCDEFGHIJKLMNOPQRSTUVWXY1234567890';
$charactersLength = strlen($characters);
$charactersLength2 = strlen($characters2);
$charactersLength3 = strlen($characters3);
for ($i = 0; $i < 2; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
for ($i = 0; $i < 2; $i++) {
$randomString .= $characters2[rand(0, $charactersLength2 - 1)];
}
$generate = $front.$randomString;
$sql="SELECT _code FROM antre_ WHERE _code = '$generate'";
if ($result=$db->get_results($sql))
{
$rowcount=count($result);
}
$loop_on++;
}
while($rowcount>0 || $loop_on==10);
return $generate;
}
Aucun commentaire:
Enregistrer un commentaire