0
votes

Good Evening,

I got a problem with the last insert id function with active record codeigniter, it works fine in mysql. But when i try it on SQL Server, the result always return false. Please kindly take a look on my code:

function transactions_start($data) {
    $this->db->insert($this->table, $data);

    $id = $this->db->insert_id();
    $q = $this->db->affected_rows();
    if ($q > 0) {
        return $id;
    } else {
        return FALSE;
    }
}

this is the result of $this->db->last_query()

select @@IDENTITY as id

the var_dump() result always returning bool(false).

I'm developing this apps using MySQL and SQL Server, so i need to keep using the active record from codeigniter.

Any help would be great, thanks in advance!

1
Where you define $this->table??? - Saty
Debug $this->db->insert($this->table, $data); by write $this->db->last_query(); die; after insertion. then see what's the problem. May be your table name is not correct. - Umair Hamid
@Saty sorry late answer, i'm defining on the construct function. private $table; - sun station
@UmairHamid okay, i'll do that. - sun station

1 Answers

0
votes

The insert_id() function returns the id of last inserted row so you just check whether it is greater than 0 or not. Use it like

function transactions_start($data) {
    $this->db->insert($this->table, $data);
    $id = $this->db->insert_id();
    if ($id > 0) {
        return $id;
    } else {
        return FALSE;
    }
}

Reference : https://ellislab.com/codeigniter/user-guide/database/helpers.html