0

I want get the last id updated in mysql, something how ...:

 $this->db->insert('mytable', $data); 
 $id = $this->db->insert_id();

Then.. $id is my last id, but I want get the "last id" in a update function of the model.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    This may help http://stackoverflow.com/questions/15492154/last-updated-row-id-in-mysql – B-and-P Mar 17 '15 at 14:52
  • 1
    Possible Duplicate: http://stackoverflow.com/questions/20310298/get-the-id-of-the-last-updated-record – CodeGodie Mar 17 '15 at 14:58
  • Then.. Codeigniter not support return a last id in a update? ... Ok... i use a query sql for obtain this value....?? – Pedro Jose Horrach Hinarejos Mar 17 '15 at 15:02
  • 1
    Surely that does not make sense. If your update targets a single row that you must already have the `id` to make the update specific. If the update will update many rows, what use is the `id` of the last update? – RiggsFolly Mar 17 '15 at 15:04
  • If you select your model, you can just use `$model->id` – Cas Bloem Mar 17 '15 at 15:13
  • If you google the question you will get lots of answer of it. You cannot get update ids after update or delete operation. – Shaiful Islam Mar 17 '15 at 15:16
  • 1
    I am guessing that you update a row, identified using columns, other than the 'id' column. i.e. 'username' etc. And, you would like the 'id' of the record to do further processing on it? There is no way to do that in one query. However, doing a couple of queries is perfectly ok normally. Select the record first then update it. – Ryan Vincent Mar 17 '15 at 15:30
  • mysqli_insert_id($con); has worked for me always – Eda190 Mar 17 '15 at 16:04

1 Answers1

0

You could try need to put a return on $id;. In your post you said you wanted to get last update id but you had insert.

Untested on update but tested on insert.

Model

public function insert() {
  $id = $this->db->insert_id() ;
  $this->db->insert('mytable'); 
  return $id;
}

public function addData($id, $data) {
  $data = array(
   'username' => $this->input->post('username')
  );
  $this->db->insert('anothertable', $data); 
}

Controller

public function index() {
$this->load->model('yourmodel');

$id = $this->yourmodel->insert();


$this->yourmodel->addData($id, $this->input->post());


}
  • he had insert as an example to how he wanted the update to work. Its not your fault, the question is not too clear. – CodeGodie Mar 17 '15 at 16:03