phpMyAdmin
 sql >> Base de données >  >> Database Tools >> phpMyAdmin

Créer un identifiant unique composé du numéro de l'année en cours et d'une valeur incrémentée automatiquement

create Table width two columns: Year,LatestNumber 

function generateStudentId(){
    $year = date("Y");
    $latestNumber = 0;
    $res = mysql_query("select * from myTable where Year=$year");
    $result = mysql_fetch_array($res);
    if($result)
        $latestNumber = $result['LatestNumber'];
    $latestNumber++;
    if($result){
        mysql_query("update myTable set LatestNumber=$latestNumber where Year=$year");
    }
    else
        mysql_query("insert into myTable values($year,$latestNumber)");

    $fiveDigitNumber = return str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);
    $studentId = $year.$fiveDigitNumber;
    return $studentId;
}