in reply to MySQL / DBI / OOP

<plug> SPOPS will help you out much here -- your $prospect object can have serialization behavior built-in, so once you've set all your properties all you need to do is:

# Save the prospect object eval { $prospect->save }; if ( $@ ) { die "Cannot save prospect:", "$SPOPS::Error::system_msg\n"; } print "Prospect ID: ", $prospect->id, "\n";
You'd need to give it some configuration information (list your fieldnames, name your primary key and table, etc.), but that's about it. Ping me for more info if you're interested. </plug>

Chris
M-x auto-bs-mode

Replies are listed 'Best First'.
Re: Re: MySQL / DBI / OOP
by rogueFalcon (Beadle) on Dec 09, 2001 at 03:52 UTC
    After spending sometime with the other perl monks here is my revised version of the same function above... please let me know what you think

    sub InsertProspect { # Get the new prospect my $prospect = shift; # Connect to the database my $dbh = DBI->connect('DBI:mysql:menagerie', 'menagerie') or die +DBI->errstr; # Insert the new prospect in the database my $sth = $dbh->prepare("INSERT INTO prospect VALUES (NULL, ?, ?, +?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); # Execute the statement $sth->execute($prospect->name, $prospect->address, $prospect->address2, $prospect->state, $prospect->city, $prospect->phone, $prospect->phone2, $prospect->phone3, $prospect->phone4, $prospect->phoneType, $prospect->phone2Type, $prospect->phone3Type, $prospect->phone4, $prospect->email, $prospect->url, $prospect->interestLevel, $prospect->designFirm, $prospect->hostingFirm, $prospect->companyName) or die DBI->errstr; # Lets get the prospect id that we just added back $sth = $dbh->prepare("SELECT last_insert_id() from prospect"); $sth->execute or die DBI->errstr; my @id = $sth->fetchrow_array; my $ID = $id[0]; # Return the new prospects ID return $ID; }
    -- rogueFalcon

    Why do you people insist on doing things sdrawkcab?