Add Prefix to auto-increment in mysql db

前端 未结 4 1722
误落风尘
误落风尘 2020-12-03 16:07

I have my database with table test1. It has a primary id \"Id\" which is auto-increment. Now the id is in the format 1,2,3.. . .Is it possible to store the primary Id as PNR

4条回答
  •  余生分开走
    2020-12-03 16:40

    Yes you can do it if you have INT prefix. You have id as INT in table

     // START PREFIX
      $query = mysql_query("SELECT id FROM `table_name` ORDER BY id DESC LIMIT 1"); 
      // GET THE LAST ID MAKE SURE IN TABLE YOU 9991
    
      while ($row = mysql_fetch_object($query)) {
        $lastId =  $row->id;
      }
    
      list($prefix,$Id) = explode('999',$lastId );
      $Id = ($Id+1);
      $new_id = '999'.$Id;
      // END PREFIX
    
    
    
    $insertQuery = mysql_query("INSERT INTO `table_name` SET id = '".$new_id."',...");
    

提交回复
热议问题