我需要从001开始的自动生成序列号,仅用于PHP/MYSQL 中的3个数字
例如:001下一个是002下一个将是003。。。009。。010。。119..120..等,但生成的数字应该是按顺序的,而不是像001、120或008等,我需要按顺序的数字。
实际上我有产品跟踪系统。我需要我的主要产品id+自动序列号的混合器(如果需要,只有3位数字可以更改),因此我的最终产品id变为:
111-001、111-002、111-003、111-004。。。。etc
注意:这个自动序列号将插入到我的mysql数据库中(在ADD按钮后面跟着更新查询(因此我的自动序列将通过更新查询输入到数据库中)
只需在id列中添加长度3和零填充即可。
ALTER TABLE `YOUR_TABLE` CHANGE
`id_column` `id_column` INT( 3 ) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT
这应该适用于您,只需稍作修改即可满足您的要求
<?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());
}
试试这个代码:
$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;
输出:00020