pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Haddon <tom@betterhealthfoundation.org>
To: pgsql-php@postgresql.org
Subject: Nextval Problems
Date: Wed, 21 Aug 2002 17:33:45 -0700
Message-ID: <NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org> (raw)

Hi Folks,

Quick question regarding inserting records into multiple tables that have
key relationships. I have a php page that sets a variable to the nextval
value, adds the record into the first table, and then inserts into the other
tables with the value that was used for the first (foreign keys).

Problem is, I have to add 1 onto the nextval to make this work, and when I
look at the ids that are being added into the tables, they are not
consecutive, one is being missed each time, so for instance I have 1000,
1002, 1004, etc.

Can anyone help? Is this something to do with the way I am connecting to the
database? I build it all the insertion pieces into one query separated by
";", of course. Originally I thought that was the problem, and split them
into separate queries and then ran them one after the other, but no dice. Am
I misunderstanding nextval?

Here's how I get the nextval

// Need to get value of next input id for agency_contact_info field
$strnextval="SELECT nextval('agency_contact_info_id_key');";

// Open Recordset
$q_nextval=pg_exec($dbconn, $strnextval)
	or die("Couldn't open the database --> " . pg_errormessage($q_nextval));

// Need to work out why this has to be '+1'
$nextid=pg_result($q_nextval,0)+1;


And here's me building the query to add in the record in the other tables

// Breast Cancer Resources
$strinsert_breast="INSERT INTO breast_cancer_resources (id) VALUES
($nextid);";


Thanks, much appreciated, Tom

_______________________________
Tom Haddon
IT Director
The Better Health Foundation
414 Thirteenth Street, Suite 450
Oakland, CA 94612
(510) 444-5096
www.betterhealthfoundation.org
_______________________________




view thread (7+ messages)  latest in thread

Message-ID: <NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org>
Permalink:  ../NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org/
Also on:    postgresql.org/message-id/NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org

 · 

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: tom@betterhealthfoundation.org
  Subject: Re: Nextval Problems
  In-Reply-To: <NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org>
  References: <NEBBIHDGCLBEJMPFAMLAKEEACIAA.tom@betterhealthfoundation.org>

* 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