$cursor = $dbh->prepare("insert into table (a, b) values (?, ?)");
$cursor->execute($a_var, $b_var);
You can then read the last autoincrementid using a MySQL extension
$id = $dbh->{mysql_insertid};
This variable is related to your dbh session so it is safe in
the presence of multiple updaters.
In a previous version of DBD::mysql the variable
was just $dbh->{insertid}.
Don't try to get the last inserted id by "select max(id) from table"
this will work short term, but get you into no end of trouble later. You could make
it work by locking the table before your insert I guess.
It would be useful if this was standardised in DBI if possible. I personally
build all my tables using autoincrementing id's and $dbh->{mysql_insertid} for joining them.
Look at your documentation for DBD::DB2 and see if
it offers anything similar! |