pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Phil Geer <philg@gearcc.com>
To: Ray Hunter <rhunter@venticon.com>
To: pgsql-php@postgresql.org
Subject: Re: serial type question
Date: Wed, 13 Nov 2002 22:53:55 -0500
Message-ID: <032801c28b91$752b5c90$4ac16dd8@offback2000> (raw)
References: <1037217368.6700.113.camel@pclnxrhunter.digitalglobe.com>

For Question #2 this is how I handle it.

$query = pg_Exec( $dbconnect,
                               "select nextval(table_name_seq);");

Store that value in a variable then you can insert it into your table1 with
the serial (PK) and into the table2 as a reference.

$query = pg_Exec( $dbconnect,
                                "insert into table1 (serial, col1, col2)
                                                values
(variable_set_by_nextval, data1, data2);
                                insert into table2 (table2_PK,  table2_col1,
table2_col_that_references_table1)
                                                values(table2_PK_data ,
table2_col1_data, variable_set_by_nextval);");



You may also want to take a look at this section of the postgresql manual
it'll be of help.

http://www.postgresql.org/idocs/index.php?functions-sequence.html

As for Question #1 it should work just fine if you do a begin and commit


$query = pg_Exec( $dbconnect,
            "BEGIN WORK;
                insert into table1 (serial, col1, col2)
                                values (variable_set_by_nextval, data1,
data2);
                insert into table2 (table2_PK,  table2_col1,
table2_col_that_references_table1)
                                values(table2_PK_data , table2_col1_data,
variable_set_by_nextval);
             COMMIT WORK;");

Good Luck
Phil

----- Original Message -----
From: "Ray Hunter" <rhunter@venticon.com>
To: <pgsql-php@postgresql.org>
Sent: Wednesday, November 13, 2002 2:56 PM
Subject: [PHP] serial type question


> I have a php script that will be inserting from a form into two tables.
>
> Table1 has a serial data type.
> Table2 references Table1's serial data type (PK).
>
> Here are my questions:
> 1. Can i run a transaction process from php? If yes, how?
> 2. How can i get the serial value for inserting into the 2nd table?
>
>
> --
> Thank you,
>
> Ray Hunter
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>




view thread (3+ messages)  latest in thread

Message-ID: <032801c28b91$752b5c90$4ac16dd8@offback2000>
Permalink:  ../032801c28b91$752b5c90$4ac16dd8@offback2000/
Also on:    postgresql.org/message-id/032801c28b91$752b5c90$4ac16dd8@offback2000

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: philg@gearcc.com, rhunter@venticon.com
  Subject: Re: serial type question
  In-Reply-To: <032801c28b91$752b5c90$4ac16dd8@offback2000>
  References: <1037217368.6700.113.camel@pclnxrhunter.digitalglobe.com> <032801c28b91$752b5c90$4ac16dd8@offback2000>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox