auto generate serial number in php code example
Example: php generate serial number
function GenerateSerial() {
$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');
$sn = '';
$max = count($chars)-1;
for($i=0;$i<20;$i++){
$sn .= (!($i % 5) && $i ? '-' : '').$chars[rand(0, $max)];
}
return $sn;
}
$serial = GenerateSerial() ;
while (checkifSerialexist($dbh, $serial))
{
$serial = GenerateSerial() ;
}
function checkifSerialexist ($dbh, $serial)
{
$statement = $dbh->prepare("SELECT `id` FROM `table` WHERE `SN` = :existSN");
$statement->bindParam(':existSN', $serial, PDO::PARAM_STR);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
$result = $statement->fetchAll();
return (count($result) > 0);
}