如何在将行添加到表中后立即引用该行


How do I reference a row immediately after it has been added to a table?

This is PHP/CodeIgniter/MySQL

我能想到的唯一方法是执行插入($this->db->insert(...)),然后立即运行另一个查询以再次查找记录。

我希望有一些东西(对我来说似乎更有效)返回新添加记录的主键(或其他东西)。

对于代码点火器

$this->db->insert_id()
http://ellislab.com/codeigniter/user_guide/database/helpers.html 看看

这里,第一个函数是$this->db->insert_id();

这也适用于活动记录插入。

或者你可以做

$last_insert_id = $this->db->call_function('insert_id');