在CodeIgniter中,有没有任何选项可以获取新记录的最后插入ID?
$last_id = $this->db->insert('tablename',
array('firstcolumn' => 'value',
'secondcolumn' => 'value')
);
假设数据表包含id(自增)、firstcolumn和secondcolumn字段。
这样,您就可以在以下代码中使用插入的id。
在CodeIgniter中,有没有任何选项可以获取新记录的最后插入ID?
$last_id = $this->db->insert('tablename',
array('firstcolumn' => 'value',
'secondcolumn' => 'value')
);
假设数据表包含id(自增)、firstcolumn和secondcolumn字段。
这样,您就可以在以下代码中使用插入的id。
最后插入的ID指使用active record中的此方法可以获取插入的自增ID。
$this->db->insert_id()
// it can be return insert id it is
// similar to the mysql_insert_id in core PHP
你可以参考这个链接,可以找到更多的资料。
对于特定的数据表,您不能使用 $this->db->insert_id()。即使最后一次插入已经很久以前,也可以像这样获取它。可能不太准确,但对我来说能够正常工作。
$this->db->select_max('{primary key}');
$result= $this->db->get('{table}')->row_array();
echo $result['{primary key}'];
以下是帮助请求ID和查询的详细信息列表:
获取最后插入的ID:这将从表中获取最后一条记录。
$this->db->insert_id();
在模态请求之后添加此SQL查询
$this->db->last_query()
$this->db->insert_id();
返回最新插入的id。$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.
$this->db->insert_id();
试一下这个。
public function insert_data_function($your_data)
{
$this->db->insert("your_table",$your_data);
$last_id = $this->db->insert_id();
return $last_id;
}
$this->db->insert_id()
//请尝试这个
if($this->db->insert('Your_tablename', $your_data)) {
return $this->db->insert_id();
}
return false