Welcome Guest, Not a member yet? Register   Sign In
Active Record "insert_id" MySQL
#1

[eluser]micha8l[/eluser]
I realised insert_id was returning 0 and I found out why.

Bad
Code:
if($this->db->affected_rows() > 0)
{
     $this->session->set_userdata(array('antispam_article' => $current_time));
     return $this->db->insert_id();
}

Bad
Code:
if($this->db->affected_rows() > 0)
{
     $insert_id = $this->db->insert_id();
     $this->session->set_userdata(array('antispam_article' => $current_time));
     return $insert_id;
}

Good
Code:
if($this->db->affected_rows() > 0)
{
     $insert_id = $this->db->insert_id();
     $this->session->set_userdata(array('antispam_article' => $current_time));
     return (string) $insert_id;
}

It seems when setting session data with CI's session -- database stored -- that it'll cause the db->insert_id() to always return a value of (int) 0. Additionally if you don't return the ID as a string it also evaluates to zero.

This is intended by CI?


Messages In This Thread
Active Record "insert_id" MySQL - by El Forum - 03-21-2012, 01:37 AM
Active Record "insert_id" MySQL - by El Forum - 03-21-2012, 03:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB