Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

sql - How to create custom function for create reference number as primary in this 2021-0001 format in php

I try to create the primary key value customizable increment format. I write PHP function to generate the primary key by using the following function.

    function create_referance_key($connect){

      /**********************create referance key********************************/
      $year = date('Y');

      $query = "
        SELECT MAX(strRefNo) AS refnum FROM claims
        WHERE strRefNo LIKE ''".$year."'%'";
     $statement = $connect->prepare($query);
     if($statement->execute())
     {   
        $result = $statement->fetchAll();
        //$RefNo = '';
        foreach($result as $row)
        {
              $RefNo = $row["refnum"];

              if($RefNo == ''){

              $RefNo = $year.'-'. str_pad(1, 4, '0', STR_PAD_LEFT);

              }else{
                 
              $array = explode("-",$RefNo);
              $RefNo = $year.'-'.str_pad($array[1] + 1, 4, 0, STR_PAD_LEFT);

              }

        }
        return $RefNo;
     }

 }

I want to create a key like 2021-0001,2021-0002,2021-0003. I try the above function.first time of execution of select query no data in the claim table .in case query return NULL. Other case query show max ref number.(both are check enter dummy data) But it does not return the any value . My SQL query part is alright. What is an error that I made inside foreach loop??

Claims table structure:

enter image description here

question from:https://stackoverflow.com/questions/65925058/how-to-create-custom-function-for-create-reference-number-as-primary-in-this-202

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

nothing wrong with your function .but

SELECT MAX(strRefNo) AS refnum FROM claims
        WHERE strRefNo LIKE ''".$year."'%';

parameter binding doing in the wrong way ''".$year."'%' should be changed as '".$year."%' .

Change function

function create_referance_key($connect, $empno){

      /**********************create referance key********************************/
      $year = date('Y');;

      $query = "
        SELECT MAX(strRefNo) AS refnum FROM claims
        WHERE strRefNo LIKE '".$year."%'";
     $statement = $connect->prepare($query);
     if($statement->execute())
     {   
        $result = $statement->fetchAll();
        //$RefNo = '';
        foreach($result as $row)
        {
              $RefNo = $row["refnum"];

              if(empty($RefNo)){

               $RefNo = $year.'-'. str_pad(1, 4, '0', STR_PAD_LEFT);

              }else{
                 
                 $array = explode("-",$RefNo);
                 $RefNo = $year.'-'.str_pad($array[1] + 1, 4, 0, STR_PAD_LEFT);

              }

        }
        return $RefNo;
     }

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...