Returns the value from the specified sequence for a newly added row.

The sequence has to have the name {table}_{column}_seq, which is the default when creating a SERIAL type.

<?php

function GetnewID($connection_id,$tablename, $fieldname)
{   
    $res = pg_exec($connection_id, 
                   "SELECT last_value FROM ${tablename}_${fieldname}_seq");  
    $seq_array    = pg_fetch_row($res, 0);  
    return $seq_array[0];
}

?>
Example:

<?php

    $connection_id = pg_connect("host={} user={} port={} password={} dbname={}");
    $res = pg_exec($connection_id,"insert into data (name) values ('New record')");
    $last_id = GetnewID($connection_id,"data", "key");

?>

returns

value of key for newly added record