0

I have a PDO connection where I am inserting a new ROW in the databse:

$sql = UPDATE invoices SET ..."

Or

$sql = INSERT INTO invoices ..."

Then:

$stmt = self::getPDOConnection(self::DEFAULT_CONNECTION_NAME)->prepare($this->sql);
$stmt->execute($this->values);

And I would like to get the id of the updated, inserted element.

Thank you.

Oliver Kucharzewski
  • 2,523
  • 4
  • 27
  • 51
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

1 Answers1

3

You can use this.

public string PDO::lastInsertId ([ string $name = NULL ] )

FYI Link

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34
  • Thank you, however, what if I have multiple users on the same project using different insertions, updates, I am sure that this will return the right id? – Milos Cuculovic Jul 07 '14 at 12:20
  • 1
    No, only the last inserted ID will be returned (this is easily tested). The only way to guarantee that you are returning the right ID to the right user is write a process or function that queries the database based on what that user just inserted into the table. http://www.php.net/manual/en/pdo.lastinsertid.php – Jay Blanchard Jul 07 '14 at 12:23