如何在codeigniter(或php)中获得插入的新id


How can i get the inserted new id in codeigniter(or php)?

例如,如果表desc为

id int(11) pk auto_increment
content varchar(128)

如果我插入了(null, 'test'),我如何获得插入的新id?

输入"test"不能是where条件,因为它不是唯一的键,所以可以重复。

有什么好主意吗?

还是我必须重新设计表格,插入一些唯一的密钥?

尝试使用类似$this->db->insert_id()

$this->db->insert(); //Here your insert query
echo "Insert Id is ".$this->db->insert_id();

试试这个LINK

您可能正在查找LAST_INSERT_ID

LAST_INSERT_ID是一个值,表示最近执行的INSERT语句为AUTO_INCREMENT列设置的第一个自动生成的值,以影响该列。它是特定于会话的。

如果你想要最后一个插入id,你可以尝试这个

$this->db->insert_id();