From: | "Pat M" <pmeloy(at)removethispart(dot)home(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: kinda newbie - ish question |
Date: | 2001-10-09 21:42:26 |
Message-ID: | 9pvr9i$1hmv$1@news.tht.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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(at)earthlink(dot)net> wrote in message
news:d44ca591(dot)0110081831(dot)e820dba(at)posting(dot)google(dot)com(dot)(dot)(dot)
> 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
From | Date | Subject | |
---|---|---|---|
Next Message | Hans-Juergen Schoenig | 2001-10-09 21:50:21 | GEQO_EFFORT |
Previous Message | Keary Suska | 2001-10-09 20:14:04 | Re: Problems installing with --with-python |