I'm writing a few DAOs in PHP with multiple database support for a script I will be releasing soon and I noticed that PostgreSQL doesn't have an easy method for fetching the last insertion ID. Although I have an idea as a work around ( not sure if PostgreSQL would support something like this ):
function get_insert_id ($table, $id)
{
$result = pg_fetch_array ($this->query ("SELECT * FROM {$table} ORDER BY {$id} DESC LIMIT 1"));
return $result[$id];
}
I think something like this would work, but the problem is that you have to specify the name of the table and the name of the id field ( if there is any )and doing so would break the API I am trying to achieve.
I have created a DB super class in PHP to set a standard API and so far I have MySQL support. I MySQL all I have to use is the following function:
function get_insert_id ()
{
return mysql_insert_id ($this->DBlink);
}
You don't need to specify anything with the above. I was wondering if there is a simple way to achieve what I want while still keeping a standard.
I think something like this will work, but I'm not quite sure as to would I should put between the '<>':
'SELECT last_value FROM <your sequence name here>'
Help?