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();
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?
source
share