0

I'm new with postgres database codeigniter

for return last id in mysql im using this in my model

public function daftar($data){
                $this->db->insert('akun', $data);
                return $this->db->insert_id();
            }

but I'm confuse how to return las id ($this->db->insert_id) in postgres?

faza
  • 249
  • 1
  • 8
  • 24

2 Answers2

4

From the CodeIgniter documentation:

If using the PDO driver with PostgreSQL, or using the Interbase driver, this function requires a $name parameter, which specifies the appropriate sequence to check for the insert id.

In your case you need return $this->db->insert_id('akun_id_akun_seq'); if "akun_id_akun_seq" is the name of the respective sequence.

Nick
  • 1,904
  • 1
  • 17
  • 34
0

If your INSERT is something like this:

INSERT INTO public."MyTable"
(
    "SomethingIdFk", 
    "Date"
) 
VALUES 
(
    8, 
    CURRENT_TIMESTAMP
);

And MyTable has a serial like MyTableId as primary key, then in your model you can do this:

$id= $this->db->insert_id('public."MyTable_MyTableId_seq"');

to get the last insert id.

That works for me.

More info you can find in this post.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
jsanmarb
  • 906
  • 12
  • 14