我想将数字从00000增加到99999.我试过了,但它没有用。以下是我的代码:
$result=mysql_query("select rid from regid") or die (mysql_error("No Record Found"));
while($row=mysql_fetch_array($result))
{
extract($row);
$sub=substr("$rid",15); //It is substring 00000 coming from database.
$n2 = str_pad($sub + 1, 5, 0, STR_PAD_LEFT); // It is code line.
echo $n2;
}
答案 0 :(得分:0)
按如下方式添加增量
$i = 0;
while($row=mysql_fetch_array($result))
{
extract($row);
$sub=substr("$rid",15); //It is substring 00000 coming from database.
$n2 = str_pad($sub + $i, 5, 0, STR_PAD_LEFT); // It is code line.
echo $n2;
$i++;
}
答案 1 :(得分:0)
只需添加另一个循环,将i +1增加到99999。
$result=mysql_query("select rid from regid") or die (mysql_error("No Record Found"));
while($row=mysql_fetch_array($result))
{
extract($row);
$sub=substr("$rid",15); //It is substring 00000 coming from database.
}
$i = 0;
while ($i<100000)
{
$n2 = str_pad($sub + $i, 5, 0, STR_PAD_LEFT);
echo $n2;
$i++;
}
答案 2 :(得分:0)
答案 3 :(得分:0)
数据库可以直接为您使用 zerofill 。如果您创建这样的表(或更改自己的表)。使用int的长度,您可以说出您想要多少位数:
MariaDB [YourSchema]> show create table yourCount;
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| yourCount | CREATE TABLE `yourCount` (
`id` int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,
`myDate` varchar(16) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 |
+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [YourSchema]>
示例 - 显示数据
MariaDB [YourSchema]> select * from yourCount;
+--------+--------+
| id | myDate |
+--------+--------+
| 000001 | data1 |
| 000002 | data2 |
| 000003 | data3 |
+--------+--------+
3 rows in set (0.00 sec)
插入不计数的新行
MariaDB [YourSchema]> insert into yourCount (myDate) VALUES ('data4');
Query OK, 1 row affected (0.06 sec)
MariaDB [YourSchema]> select * from yourCount;
+--------+--------+
| id | myDate |
+--------+--------+
| 000001 | data1 |
| 000002 | data2 |
| 000003 | data3 |
| 000004 | data4 |
+--------+--------+
4 rows in set (0.00 sec)
添加ID为
的行MariaDB [YourSchema]> insert into yourCount (id,myDate) VALUES (5,'data4');
Query OK, 1 row affected (0.06 sec)
MariaDB [YourSchema]> select * from yourCount;
+--------+--------+
| id | myDate |
+--------+--------+
| 000001 | data1 |
| 000002 | data2 |
| 000003 | data3 |
| 000004 | data4 |
| 000005 | data4 |
+--------+--------+
5 rows in set (0.00 sec)