I am copying a row in one of the tables in my database and trying to get the last inserted ID. Copying the row works fine, and the AI field updates properly, but it is not returning the last insert ID (although it does return true - or if I echo something else out in its place that works fine as well).
Some of the other queries also use last insert ID and those work well.
I suspect it is something to do with the temporary table, but cannot find anything on SO about a similar problem with last insert ID and temporary tables. Does anyone know what is causing the problem or if there is a workaround?
public function duplicateItinRow($itineraryID){
$this->db->query("CREATE TEMPORARY TABLE temporary_itin_table AS SELECT * FROM itinerary_ref WHERE Itinerary_ID = $itineraryID;
UPDATE temporary_itin_table SET Itinerary_ID=NULL;
INSERT INTO itinerary_ref SELECT * FROM temporary_itin_table;
DROP TEMPORARY TABLE temporary_itin_table");
//Execute
if($this->db->execute()){
return $this->db->lastInsertId();
} else {
return false;
}
}
UPDATE:
Not the ideal solution I'm sure, but have got it working using the copy method detailed here: How to copy a row and insert in same table with a autoincrement field in MySQL?