从 001 开始自动生成的序列号(仅适用于 3 位数字)-PHP/MYSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20283458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:43  来源:igfitidea点击:

Auto generated sequence number staring from 001 ( ONLY FOR 3 DIGITS) - PHP / MYSQL

phpmysqlnumberssequence

提问by

I need Auto generated sequence number staring from 001 ONLY FOR 3 DIGITS in PHP / MYSQL

我需要自动生成的序列号从 001 开始,仅用于 PHP/MYSQL 中的 3 位

ex: 001 next will be 002 and next will be 003 ... 009 .. 010 ..119..120..etc but number generated should be in sequence wise not like 001 then 120 or 008 etc , i need in sequence number.

例如:001 下一个将是 002,下一个将是 003 ... 009 .. 010 ..119..120..etc 但生成的数字应该按顺序排列,而不是像 001 然后是 120 或 008 等,我需要序列号.

actually i have product tracking system . where i need mixer of my main product id + auto sequence number ( only 3 digit can change if wanted ) hence my final product id becomes :

实际上我有产品跟踪系统。我需要混合我的主要产品 ID + 自动序列号(如果需要,只能更改 3 位数字),因此我的最终产品 ID 变为:

111-001 , 111-002 , 111-003 , 111-004 ....etc

111-001 , 111-002 , 111-003 , 111-004 ....等

Note: this auto sequence number will be insert in my mysql database ( after ADD button follows by Update query ( hence my auto sequence will be enter in database with Update query ))

注意:此自动序列号将插入到我的 mysql 数据库中(在 ADD 按钮之后是更新查询(因此我的自动序列将通过更新查询输入到数据库中))

采纳答案by bitWorking

Just add the length 3 and zerofill to your id column.

只需将长度 3 和 zerofill 添加到您的 id 列。

ALTER TABLE  `YOUR_TABLE` CHANGE  
`id_column`  `id_column` INT( 3 ) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT

回答by Allen Esteban

Try this code:

试试这个代码:

$maxDigit=5; //maximum digit of ur number
$currentNumber=0020;  //example this is your last number in database all we need to do is remove the zero before the number . i dont know how to remove it
$count=strlen($currentNumber); //count the digit of number. example we already removed the zero before the number. the answer here is 2
$numZero="";
for($count;$count<=$maxDigit;$count++)
     $numZero=$numZero+"0";

$newNum=$newNum+$currentNumber;


Output :00020

输出:00020

回答by praveen

This should work for you with slight modifications to fit ur requirement

这应该对您有用,只需稍作修改即可满足您的要求

 <?php
$connection = mysql_connect("localhost","root","")
or die("no connection");
$db_select=mysql_select_db("test",$connection)
or die("no connection to db");
$query3 = "SELECT * FROM simple";
$result3 = mysql_query($query3);
$count = mysql_num_rows($result3);
if($count == 0)
{
$seq = 1;
$ref = 111;
$a = sprintf("%04d", $seq);
$emp_id = $ref.'-'.$a;
$query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
$result=mysql_query($query)or die(mysql_error());
}
else
{
    $query2 = "SELECT * FROM simple ORDER BY id DESC LIMIT 1";
    $result2 = mysql_query($query2);
    $row = mysql_fetch_array($result2);
    $last_id = $row['emp_id'];
    $rest = substr("$last_id", -4);  
    $insert_id = "$rest" + 1;
    echo $ars = sprintf("%04d", $insert_id);
    $ref = 111;
    $emp_id = $ref.'-'.$ars;
    $query= "INSERT INTO simple (emp_id) VALUES ('$emp_id')";
    $result=mysql_query($query)or die(mysql_error());
}