On Wed, Oct 22, 2008 at 3:18 PM, thebigdog <[EMAIL PROTECTED]> wrote:
>> I'm not having any luck finding the equivalent to mysqli_insert_id() for
>> postgres. I've seen the method of getting the id from the sequence, but I'm
>> hoping for a better way to do then that...
>
> technically, there is not a php function in postgesql like that function. you
> can write a query to provide that information based on the sequence that you
> are
> using.
>
> select currval('sequence_name');
>
> that will return the last value used. however, there is not a php function
> that
> will provide the functionality that mysqli_insert_id() does.
Eeek. That's bad. Don't do that.
You are exposing a race condition where the sequence may be
incremented by another INSERT before you get a chance to query the
current value.
Like Joseph said, If you need to have the last ID in your code, use
the old-school method:
1. Request new ID from the sequence
SELECT nextval('sequencename');
2. Insert your record using this value.
INSERT INTO table (id, col, col, col) VALUES ($id, ....
If you don't need the last ID in your code, you can use the new method
(new to Pg that is) which is just like MySQL.
Declaring the ID column as SERIAL is what gives you the automatic
sequence magic like MySQL.
http://www.postgresql.org/docs/8.1/static/datatype.html#DATATYPE-SERIAL
--lonnie
_______________________________________________
UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net