Getting last inserted row after executing INSERT in PDO (with MySQL)?

when inserting a new object (as a string) into a MySQL database, I want to get the last inserted row back (id is NOT always enough, since I want to use this in common functions where some INSERTs don't have automatically incrementing identifiers).

I need this to bring the whole object back to success using an identifier, a POSSIBLY database timestamp, or in some cases only the values ​​that I knew before inserting, so PDO :: lastInsertId does not solve the problem.

$query = "INSERT INTO table (".$colinsert.") VALUES (".$colbind.")";
$stmt = $this->db->prepare($query);
$ex = $stmt->execute();
// ... if $ex was successful, how do I retrieve the last inserted row now?

Regardless of the database, the solution would be nice, but if it depends on MySQL, this is also fine :-)

Edit: To prevent some misunderstandings - for example, I have a user_contact_connection table with the following lines (contact_id, user_id, extra_connection_info). The primary key is (contact_id, user_id). PDO :: lastInsertId returns 0 in this case, since there is no auto-increment column.

Ok in this case, I KNOW which row I am inserting, however I still hope to find a way to simply get the last inserted row in the general case without explicitly asking for identifiers? Is it possible?

+4
source share
1 answer

db , , db (, , insert, MySQL NOW() ..), , . , ORM, ​​ Doctrine, , , .. ( ).

0

All Articles