php CodeIgniter activerecord,检索最后插入的ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1985967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter activerecord, retrieve last insert id?
提问by Dennis Decoene
Are there any options to get the last insert id of a new record in CodeIgniter?
是否有任何选项可以在 CodeIgniter 中获取新记录的最后一个插入 ID?
$last_id = $this->db->insert('tablename',
array('firstcolumn' => 'value',
'secondcolumn' => 'value')
);
Considering the table consits of fields id (autoincrement) firstcolumn and secondcolumn.
考虑到字段 id(自动增量)firstcolumn 和 secondcolumn 的表构成。
This way you can use the insert id in the following code.
这样您就可以在以下代码中使用插入 ID。
回答by Dennis Decoene
Shame on me...
羡慕我...
I looked at the user guideand the first function is $this->db->insert_id();
我查看了用户指南,第一个功能是$this->db->insert_id();
This also works with activerecord inserts...
这也适用于活动记录插入...
EDIT: I updated the link
编辑:我更新了链接
回答by Raja Ram T
Last insert id means you can get inserted auto increment id by using this method in active record,
最后插入 id 意味着您可以通过在活动记录中使用此方法来获取插入的自动增量 id,
$this->db->insert_id()
// it can be return insert id it is
// similar to the mysql_insert_id in core PHP
You can refer this link you can find some more stuff.
你可以参考这个链接,你可以找到更多的东西。
回答by Infant Rosario
for Specific table you cannot use $this->db->insert_id() . even the last insert happened long ago it can be fetched like this. may be wrong. but working well for me
对于特定表,您不能使用 $this->db->insert_id() 。即使最后一次插入发生在很久以前,也可以像这样获取。可能是错误的。但对我来说效果很好
$this->db->select_max('{primary key}');
$result= $this->db->get('{table}')->row_array();
echo $result['{primary key}'];
回答by Madhur
List of details which helps in requesting id and queries are
有助于请求 id 和查询的详细信息列表是
For fetching Last inserted Id :This will fetching the last records from the table
获取上次插入的 ID :这将从表中获取最后一条记录
$this->db->insert_id();
Fetching SQL query add this after modal request
获取 SQL 查询在模态请求后添加此项
$this->db->last_query()
回答by Nikkolai Fernandez
After your insert query, use this command $this->db->insert_id();to return the last inserted id.
插入查询后,使用此命令$this->db->insert_id();返回最后插入的 id。
For example:
例如:
$this->db->insert('Your_tablename', $your_data);
$last_id = $this->db->insert_id();
echo $last_id // assume that the last id from the table is 1, after the insert query this value will be 2.
回答by Akash Tyagi
$this->db->insert_id();
$this->db->insert_id();
Try this As your desired question.
试试这个作为你想要的问题。
回答by Raham
Try this.
尝试这个。
public function insert_data_function($your_data)
{
$this->db->insert("your_table",$your_data);
$last_id = $this->db->insert_id();
return $last_id;
}
回答by Shaiful Islam
$this->db->insert_id();
Returns the insert ID number when performing database inserts
执行数据库插入时返回插入 ID 号
Query Helper Methodsfor codeigniter-3
codeigniter-3 的查询辅助方法
回答by Girish Ninama
$this->db->insert_id()
//please try this
//请试试这个
回答by Nakendra Pun
if($this->db->insert('Your_tablename', $your_data)) {
return $this->db->insert_id();
}
return false

