【发布时间】:2019-01-12 12:35:49
【问题描述】:
我是Codeigniter 的新手。如果不在数据库中使用自动增量,我想手动生成自动增量,例如 A00001, A00002, A00003 .......
如何使用 codeigniter 和 mysql 数据库生成这种类型的 ID?
【问题讨论】:
标签: mysql codeigniter auto-increment
我是Codeigniter 的新手。如果不在数据库中使用自动增量,我想手动生成自动增量,例如 A00001, A00002, A00003 .......
如何使用 codeigniter 和 mysql 数据库生成这种类型的 ID?
【问题讨论】:
标签: mysql codeigniter auto-increment
实际上,自动自增更适合主键。但是如果你想生成它,你可以这样做
function GenerateId() {
$query = $this->db->select('ID')
->from('table_name')
->get();
$row = $query->last_row();
if($row){
$idPostfix = (int)substr($row->ID,1)+1;
$nextId = 'A'.STR_PAD((string)$idPostfix,5,"0",STR_PAD_LEFT);
}
else{$nextId = 'A00001';} // For the first time
return $nextId;
}
【讨论】: