NYCPHP Meetup

NYPHP.org

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

Mitch Pirtle mitchy at spacemonkeylabs.com
Mon Aug 2 21:34:44 EDT 2004


Hey Dan,

Congrats for making it to the International PHP Conference final 40!  I 
wager with all of the submissions they limited most everything to 
PHP-specific stuff, as the HORDE gang didn't make it in either.  Good 
job, you are definitely one of the elite!  :-)  As for me and my two 
submissions, well, erm, *cough*

Daniel Convissor wrote:

>That's odd.  I'm running 7.4.1 and everything is fine on my tests.  
>Can you please run the 05sequences.phpt test?  It's located in 
><pear>/tests/DB/tests/driver or there about depending on your 
>installation.
>
>Instructions are here: http://cvs.php.net/co.php/pear/DB/doc/TESTERS
>  
>

Spent some time on this, but on a RH7.3 box with PHP handmade it was not 
possible for me to figure out.  None of the test stuff was installed 
(and was still hanging around in the source directory trees), and the 
php executable was nowhere near the source directories either, for 
obvious reasons ;-)

>I trust you've broken down your problem into a simple test script.  If 
>so, email it to me and I'll check it out.
>  
>

Here is the function that does the update, and should be easy to understand:

    function processForm(){
        // get the next sequence id for content_archives
        $id = $this->db->nextId( 'content_archives_id_seq' );
        if (DB::isError($id)) {
            die($id->getMessage());
        }
        // build insert SQL
        $sql = 'INSERT INTO content_archives (content_id, created, 
title, content_data, staff_id) VALUES (' .
            $_POST['content_id'] . ', now(), ' . $this->db->quoteSmart( 
$_POST['title'] ) . ', ' .
            $this->db->quoteSmart( $_POST['content'] ) . ', ' . 
$_POST['staff_id'] . ')';
        $res = $this->db->query( $sql );
        if (DB::isError($res)) {
            die($res->getMessage());
        }
        // update content to point to new content_archives.id
        $sql = 'UPDATE content SET content_archive_id = ' . $id . ' 
WHERE id = ' . $_POST['content_id'];
        $res = $this->db->query( $sql );
        if (DB::isError($res)) {
            die($res->getMessage());
        }
        echo "<p>Content update successful!</p>\n";
    } // ends function processForm

Whenever this function is called, the last insert gets an older id from 
the content_archives_id_seq sequence, most definitely not current.  As 
this is new code that is only accessable on a development site, I am the 
only person running it at present - so there's no possibility of the 
sequence getting incremented while the process is underway.

What do you think?  Am I not using nextID() properly here?  Am I missing 
a commit that needs to happen first?

-- Mitch, suddenly annoyed with sequences



More information about the talk mailing list