php generate serial number

Solutions on MaxInterview for php generate serial number by the best coders in the world

showing results for - "php generate serial number"
Tiphaine
10 Jan 2017
1function GenerateSerial() {
2  $chars = array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
3  $sn = '';
4  $max = count($chars)-1;
5  for($i=0;$i<20;$i++){
6   	$sn .= (!($i % 5) && $i ? '-' : '').$chars[rand(0, $max)];
7  }
8  return $sn;
9}
10
11$serial = GenerateSerial() ;
12
13while (checkifSerialexist($dbh, $serial))
14{
15   $serial = GenerateSerial() ;
16}
17
18function checkifSerialexist ($dbh, $serial)
19{
20  $statement = $dbh->prepare("SELECT `id` FROM `table` WHERE `SN` = :existSN");
21  $statement->bindParam(':existSN',  $serial, PDO::PARAM_STR); 
22  $statement->execute();
23  $statement->setFetchMode(PDO::FETCH_ASSOC);
24  $result = $statement->fetchAll();
25
26  return (count($result) > 0);   
27}