Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the insert and update ID with PDO

Tags:

php

pdo

Im trying to get the mysql_insert_id using PDO. Thus far i've not found a good example that works with both inserting and updating. Does anyone have a piece of complete code as example?

like image 301
Sander Mangel Avatar asked Oct 18 '25 19:10

Sander Mangel


2 Answers

If you need $pdo->lastInsertId() to return the id of the row just updated (I would seriously reconsider my design if I needed it but hey... can be a true need), do the update like this:

 UPDATE tablename  SET some_column='somevalue',id=LAST_INSERT_ID(id);
like image 73
Wrikken Avatar answered Oct 20 '25 09:10

Wrikken


PDO::lastInsertId. Returns the ID of the last inserted row or sequence value

like image 41
Wahyu Kristianto Avatar answered Oct 20 '25 10:10

Wahyu Kristianto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!