Re: kinda newbie - ish question - Mailing list pgsql-general

From Pat M
Subject Re: kinda newbie - ish question
Date
Msg-id 9pvr9i$1hmv$1@news.tht.net
Whole thread Raw
In response to kinda newbie - ish question  (mmacie@earthlink.net (Mike Judkins))
List pgsql-general
I'm taking a guess, but it sounds like you want to insert a row with a URL
plus the ID of the record the URL is stored under? If so, there are a couple
ways you can accomplish this. Take a look at create sequence in the docs for
an explanation.

One method is not to create the table with a serial ID. Create it with an
int and use a manual sequence. That way you get the ID value before
inserting the row. A bit more work to set up the table, but less work later.

If you want to stick with the serial type, you'll have to use pg_getlastoid
and look up the ID of that row. Less work setting up the table, but more
work in your scripts. Here's an example

$check=pg_Exec($connection,"create table mytable(my_id serial primary
key,my_url char(50);");
$check=pg_Exec($connection,"INSERT INTO mytable(my_url) values(null);");
$oid=pg_GetLastOid($check);
$idrow=pg_Exec($connection,"SELECT my_id FROM mytable WHERE oid='$oid';");
$id=pg_Result($idrow,0,'my_id');
$url=$url.$id;
$check=pg_Exec($connection,"UPDATE mytable SET my_url='$url' WHERE
my_id='$id';");

Of course, you could also use WHERE oid='$oid' instead. Just remember, you
can't count on an OID remaining the same all the time. Don't try to use OIDs
like a primary key. Every time postgres is started the oid could point to a
different object or even no object at all. Quite safe to use in the above
manner though.


"Mike Judkins" <mmacie@earthlink.net> wrote in message
news:d44ca591.0110081831.e820dba@posting.google.com...
> hi all,
>
> Im trying to insert a record with a php script. I insert a NULL value
> to get my auto increment unique key to automatically populate as
> usual. Then I want to be able to insert another value in this same row
> which is a URL. This URL is basically a path plus a filename which I
> want to have the exact same name as the unique key that was just
> generated for the row. Is there a quick way is SQL to do this or will
> I have to handle it in my script?
>
> Thanks for helping!
>
> Mike



pgsql-general by date:

Previous
From: steve@thornet.co.uk (Steve Heaven)
Date:
Subject: pop3 server and Postgresql
Next
From: Eric D Nielsen
Date:
Subject: Postgres --with-python problems..