Yendor has asked for the wisdom of the Perl Monks concerning the following question:
After validating and un-tainting user input, I use the DBI to insert the information into my Postgres table like this:CUSTOMER_INFORMATION -------------------- id (auto-incrementing sequence, not null) first_name last_name address_1 address_2 city state country postal_code ...etc...
This code works (well, ok...The real code that I'm executing in my real script works. There might be some translation errors from script to PerlMonks in what's above.) What I'd like to know is the value of the "ID" column for the row that was just inserted. I have done this in other scripts by using what feels to me like a hack:$dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1, PrintError => 1, AutoCommit = +> 1 }); $sql = $dbh->prepare_cached(' INSERT INTO Customer_Information ( First_Name, Last_Name, Address_1, Address_2, City, State, Postal_Code ) VALUES ( ?, ?, ?, ?, ?, ?, ?); '); $sql->execute($FName, $LName, $Address1, $Address2, $City, $State, $PostalCode);
Store the result of that into a variable, and voila, I have the ID I'm looking for. I need this ID to insert as a foreign key into another table immediately after this insert is run. Is there a better way to do this? Thanks kindly.select max(id) from customer_information where first_name = $FName and + last_name = $LName;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting ID of last row inserted into database
by Corion (Patriarch) on Jan 25, 2006 at 18:46 UTC | |
by VSarkiss (Monsignor) on Jan 25, 2006 at 20:24 UTC | |
by lima1 (Curate) on Jan 25, 2006 at 21:04 UTC | |
by Yendor (Pilgrim) on Jan 26, 2006 at 15:20 UTC | |
|
Re: Getting ID of last row inserted into database
by saberworks (Curate) on Jan 25, 2006 at 20:21 UTC | |
by Yendor (Pilgrim) on Jan 26, 2006 at 15:15 UTC | |
|
Re: Getting ID of last row inserted into database
by runrig (Abbot) on Jan 25, 2006 at 22:35 UTC |