tags:

views:

48

answers:

1

Example: I insert a row into the DB with this, using PHP's built in PDO:

$sql = "INSERT INTO mytable (name, ok) VALUES ('john', '1')";
$this->dbh->exec($sql);

I need the id of that row. How could I get that?

+3  A: 

If the id is an auto_increment, you can use PDO::lastInsertId :

Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.


So, in your case, something like this should do the trick :

$lastId = $this->dbh->lastInsertId();
Pascal MARTIN