Re: Re: Getting last record ID created from DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, March 18, 2007 4:14 am, Jim Lucas wrote:
>> There in lies the biggest problem with LAMP, and that's MySQL. The
>> architecture of your methodology *only* works with MySQL, and not
>> more
>> capable databases like Oracle, DB2, or even PostgreSQL.
> I too thought the same thing, but was corrected when I starting
> working
> on a redesign for the billing system for the company that I am working
> for.  They use PostgreSQL and we do have a auto incremented unique id
> field.  It isn't call AUTO_INCREMENT, but rather a sequence value.  It
> is a value that is controlled by the default value entry.
>
> It then runs this:
> 	nextval('customers_customer_id_seq'::regclass)
>
> and uses this as the value of the id field for the newly inserted row.
>
> Then instead of running a command like SELECT LAST_INSERT_ID(); you
> have
> to do some other stuff like this:
>
> $SQL = "SELECT currval('{$table}_{$column}_seq');";

If you haven't turned OFF the built-in OID, you can also use
http://php.net/pg_last_oid to get the PostgreSQL internal OID (kind of
like a built-in auto_increment on each table, only not) and then use
that in a query to find your own ID by matching the built-in OID
field:

$query = "insert whatever into yeah ";
pg_exec($connection, $query) or die(pg_last_error($connection));
$oid = pg_last_oid($connection);
$query = "select whatever_id from yeah where oid = $oid";
$whatever_id = pg_exec($connection, $query) or
die(pg_last_error($connection));
$whatever_id = pg_result($whatever_id, 0, 0);

Same thing.

Different spelling.

They all do the same thing, pretty much, one way or another, no matter
which db you use.

And none of them match the other.

Well, okay, MS SQL and Sybase are probably the same, since MS bought
their code from Sybase originally.

And Oracle and SQL Server often end up supporting each other's syntax
oddities eventually, as one of them is trying to steal the other's
clients.

But God forbid they should just standardize on a single syntax/mechanism.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux