NYCPHP Meetup

NYPHP.org

[nycphp-talk] PEAR::DB->nextID problems

Mitch Pirtle mitchy at spacemonkeylabs.com
Tue Aug 3 12:17:51 EDT 2004


Mitch Pirtle wrote:

> Okay, first will experiment with the sequence names and check that 
> things are on the up and up.  If after that I still have trouble I'll 
> write something up that you can see.


Attached is the test script, looks a little nonsensical now that I 
understand how nextID() works.  Here was the misunderstanding, and 
hopefully you could add something to the docs to clarify?

nextID() grabs the next value from the sequence, AND THEN INCREMENTS 
THAT SEQUENCE.  So you cannot get a value from nextID() and then do an 
insert without specifying that value for the column that the sequence is 
used for.

I created a table that only had two columns, id and sequence_value.  I 
then got the latest sequence value with nextID, and inserted a new row 
like this:

    "INSERT INTO test ( sequence_value ) VALUES ( $id )";

Of course I was not thinking that I needed to use $ID for the first 
column in the table as well, and got two different values on the 
insert.  Basically row 2 got an id of 1, row 4 got an id of 3, and so 
on.  I corrected the SQL to:

    "INSERT INTO test ( id, sequence_value ) VALUES ( $id, $id )";

And of course everything syncs up properly.  The sequence is also now 
happy, as it isn't being used twice for each row.  Providing an example 
in the docs would be a time saver and avoid unnecessary forehead 
slapping.  ;-)

-- Mitch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20040803/5f7679fc/attachment.html>


More information about the talk mailing list