MySQL PDO LastInsertID after insert with “before” trigger returns wrong value

StackOverflow https://stackoverflow.com/questions/8766213

  •  14-04-2021
  •  | 
  •  

Domanda

I have a customers table with ID (auto_inc), name, ... and customer_code fields. This table has also a "before" trigger, the select one customer code from another table, and set the customer_code field before insert a new record in this table. Works perfectly.

The problem is that when I retrive the lastInsertID() it always return 1 !

I'm using PDO and PHP.

Thanks for any help!

È stato utile?

Soluzione

What version of MySQL are you using? Since 5.0.12, the last insert id shouldn't be overwritten by a trigger. But before that version, a trigger could change the value and subsequent queries would see the wrong value.

See http://dev.mysql.com/doc/refman/5.0/en/stored-routines-last-insert-id.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top