Elmer Lin
Elmer Lin

Reputation: 25

SQL Server last insert id return array in CodeIgniter

i'm using sqlsrv driver in CI However, when i use the $this->db->insert_id() it returns me an array

I'm doing this in the controller after a db insert

Error Number: 42S22

[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'Array'.

Anybody can help? Thanks alot

Model
function insert_message($data)
{
 $this->db->insert('message', $data);
 $last_id = $this->db->insert_id();
 return $last_id;
}   

Controller
$message = $this->Message->insert_message(
$data = array(
        'admin_account_id'        => $accountid,
        'message_subject'     => $subject,
        'message_content'     => $content,
        'message_priority'    => $priority,
        'date_send'           => $date_send,
        'message_status'      => $status,
        'message_createddate' => date("Y-m-d H:i:s"),
        'message_send_usergroup'=> $usergroup
    );

);


Database driver (sqlsrv_driver.php)
function insert_id()
{

    return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
}

the $data does not include the identity column. This is the code i'm using now but i'm still getting an Array from $last_id.

Upvotes: 0

Views: 3093

Answers (1)

Jorge SB
Jorge SB

Reputation: 155

Instead of :

return $this->db->insert_id();

you would replace with :

$query = $this->db->query("SELECT IDENT_CURRENT('<table_name>') as last_id");
$res = $query->result();
return $res[0]->last_id;

This is a provisional solution.

Upvotes: 2

Related Questions