phpMyAdmin
 sql >> Base de Dados >  >> Database Tools >> phpMyAdmin

Crie um ID exclusivo composto pelo número do ano atual e um valor incrementado automaticamente

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;
}