Hi there.
I use db_pgsql.inc from phplib to access postgresql.
With mysql, there is a function named insert_id that gives the values of the primary key for the last inserted tuple.
With postgresql, there is no such function. However as illustrated here : <!-- m --><a class="postlink" href="http://www.phpbuilder.com/snippet/download.php?type=snippet&id=34">http://www.phpbuilder.com/snippet/downl ... ppet&id=34</a><!-- m --> one can cope with this problem using pg_getlastoid
So i have put this in the DB_SQL class definition:
function getlastoid(){
return @pg_getlastoid($this->Query_ID);
}
I can use it this way:
$db->query("insert into users (name,age) values ('foo','12')");
$oid=$db->getlastoid();
$db->query("select id_user from users where oid='$oid'");
$db->next_record();
$id=$db->f("id_user");
I m looking for a way to get the good id value without having to execute the second query outside the sql class.
The problem seems to be that i cannot know the name of the last table used for an insert and the name of the associated primary key in the db_sql class.
If there were a way to do this, i would be able to make the code directy in the class and to code a nice insert_id function.
Any idea ?
I use db_pgsql.inc from phplib to access postgresql.
With mysql, there is a function named insert_id that gives the values of the primary key for the last inserted tuple.
With postgresql, there is no such function. However as illustrated here : <!-- m --><a class="postlink" href="http://www.phpbuilder.com/snippet/download.php?type=snippet&id=34">http://www.phpbuilder.com/snippet/downl ... ppet&id=34</a><!-- m --> one can cope with this problem using pg_getlastoid
So i have put this in the DB_SQL class definition:
function getlastoid(){
return @pg_getlastoid($this->Query_ID);
}
I can use it this way:
$db->query("insert into users (name,age) values ('foo','12')");
$oid=$db->getlastoid();
$db->query("select id_user from users where oid='$oid'");
$db->next_record();
$id=$db->f("id_user");
I m looking for a way to get the good id value without having to execute the second query outside the sql class.
The problem seems to be that i cannot know the name of the last table used for an insert and the name of the associated primary key in the db_sql class.
If there were a way to do this, i would be able to make the code directy in the class and to code a nice insert_id function.
Any idea ?