试试这样:
public function save($table, $data)
{
$res = $this->db->insert($table, $data);
//First select id attribute name
$query = $this->db->get($table);
$row = array_keys($query->row_array());
$id_attr = $row[0];
//Last Inserted id
if($res)
{
$query2 = $this->db->query("select MAX($id_attr) from $table");
$row2 = array_values($query2->row_array());
$id = $row2[0];
return $id;
}
else
{
return false;
}
}
或者
function insert($tableName, $post)
{
//First insert data
$res = $this->db->insert($tableName, $post);
//Get inserted id
if($res){
$row = $this->db->query("SELECT ID FROM $tableName ORDER BY ID DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY")->row();
$id = $row->ID;
return $id;
}else{
return false;
}
}