$new_id = $sth->{'mysql_insertid'};is the identifier you want. A database handle can also access the property.
After Compline, Zaxo
| [reply] [d/l] |
This approach may be a little filthy but it does the job fine and dandy -
$id = $dbh->selectrow_array('SELECT LAST_INSERT_ID()');
What's happening is it's using the awfully handy LAST_INSERT_ID() function in MySQL to get the id of the last autoincremented row, and selectrow_array() returns the value in the first row when called in a scalar context (we love, wantarray(), yes we do).
HTH
broquaint | [reply] [d/l] [select] |
I mainly use SQL 2000 and Access for my DB stuff, so don't mind my lack of knowledge of MySQL. That aside, does MySQL support stored procedures? It would probably be a bit faster if you made a stored procedure that just returned the last record.
Rather then looking up the ID of the last record, and then retrieving it. | [reply] |
i mainly use oracle or sometimes postgres (im not really a big mysql fan either), although mysql's dbd driver has lots of nice mysql-specific functionality; here's a rather promising implementation of stored procs, and in perl at that :
check it out
| [reply] |
are you actually inserting first? you'll need to perform an operation in order for that statement handle to retrieve the last sequence id. | [reply] |