Getting the Last Insert Id

Previously in mysql_* you did something like this.


$result = mysql_query("INSERT INTO table(firstname, lastname) VALUES('John', 'Doe')") or die("Insert Failed ".mysql_error());

$insert_id = mysql_insert_id();
With PDO you just do run the lastInsertId method.

$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

$result = $db->exec("INSERT INTO table(firstname, lastname) VAULES('John', 'Doe')");

$insertId = $db->lastInsertId();

Loading ...

Related Results :

  1. Getting the Last Insert Id
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of
  • Any registered user can create related posts based on search term tags.

About the Author