note 73163 deleted from function.PDO-lastInsertId by nlopess
| From: | nlopess@php.net | Date: | Sun, 11 Feb 2007 19:00:11 +0000 |
| Subject: | note 73163 deleted from function.PDO-lastInsertId by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123258@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at webmaster-debutant dot net
----
This example shows you how to get the last inserted ID with the PDO extension and a MySQL database.
<?php
try
{
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$statement = $dbh->prepare('INSERT INTO foobar (foo, bar) VALUES(:foo, :bar)');
$statement->bindParam(':foo', 'Hello', PDO::PARAM_STR);
$statement->bindParam(':bar', 'World', PDO::PARAM_STR);
$statement->execute();
echo 'Insertion réussie !<br/>';
echo 'Dernier ID auto-incrémenté : ', $dbh->lastInsertId();
$statement = null;
$dbh = null;
}
catch (PDOException $e)
{
echo 'Erreur ! : ', $e->getMessage() ,'<br/>';
}
?>