• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PostGreSQL $this->db->insert_id()

[eluser]Greg Aker[/eluser]
Hey guys,

I have run into a problem with postgres 7.4.7 and getting the insert_id properly returned. I came up with a workaround, but it's not going to work with php4.

I thought I'd share my findings and see if anyone can add to it.



was not working for me. a var_dump or print_r gave me the value of "S".

so I changed it to:

function _version()
    return pg_version();

So that is where the php4 vs php5 issues come in to play.

Then, in the MSSQL driver, there is a function to grab the major version. I used a modified version of that to give me the major and minor version, and this is what I came up with.

function _parse_version($version)
    $v = $version['server'];

    preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $v, $ver_info);
    $ver = $ver_info[1] . '.' . $ver_info[2];
    return $ver;

The I changed the top of insert_id() to:

function insert_id()
    $v = $this->_parse_version($this->_version());

So that's what I have and it's working nicely for me on pg 7.4.7. I'd like to see what you guys think, and I appreciate any feedback.



[eluser]Pascal Kriete[/eluser]
I don't have a postgre setup to test on, but that looks good to me. It could check for php5 and return 0 for php 4 (anything smaller than 8 should work).

A quick look at how cake and zend do it makes me think that CI might to be taking the complicated (read: ├╝bercompatible) route again.

The other two essentially do:
$seq = $column ? $table.'_'.$column.'_seq' : $table.'_seq';

$query = "SELECT CURRVAL({$seq}) as ins_id";
$query = $this->query($sql);
$row = $query->row();
return $row->ins_id;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme ┬ę 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.