$sth = $dbh->prepare("insert foo(...) values(?, ?, ...)"); # insert first row: $sth->execute('foo', 'bar', ...); # fetch the id value (assuming "foo_id" is an identity column) $sth2 = $dbh->prepare("select max(foo_id) from foo"); $sth2->execute; while($data = $sth2->fetch) { $last_id = $data->[0]; } # insert second row $sth->execute('baz', ...); # fetch the id value $sth2 = $dbh->prepare("select max(foo_id) from foo"); $sth2->execute; while($data = $sth2->fetch) { $last_id = $data->[0]; } # etc.