Aaron Yodaiken
Aaron Yodaiken

Reputation: 19551

get autoincrement value reliably across sqlite3 and mysql

Is there some standard way to get the last AUTOINCREMENT'd value which works in both MySQL and SQLite3?

I'm using PHP, so if there's some PDO way to do it, that's fine.


Please help me with an example in your answer, not just a link, because I'm a bit of a novice at the whole SQL DB thing.

Upvotes: 2

Views: 255

Answers (1)

Tim Lytle
Tim Lytle

Reputation: 17624

Have you tried PDO::lastInsertId() on both databases?

//mysql
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
//insert something
echo $db->lastInsertId();

//sqlite
$db = new PDO('sqlite:/tmp/foo.db'); 
//insert something
echo $db->lastInsertId();

It should be noted, I haven't. I figure you're curious enough to try it out and report back.

Upvotes: 1

Related Questions