views:

30

answers:

2

Is there a way to insert into MySQL database using PHP PDO and return its id? Or I will just have to search fot its id after insertion?

+3  A: 
$pdo_object->lastInsertId();

See documentation

Chacha102
what happen if there is concurrent insertion?
Phen
@Phen: That's another question entirely. @Chacha102 provided an excellent answer to *this* question already.
Jonathan Sampson
You might want to check out this comment about thread-safeness: http://us2.php.net/manual/en/pdo.lastinsertid.php#84530 (Just a quick Ctrl+F search on the Docuementation for the word 'safe')
Chacha102
+1 for being quicker on the draw, but shouldn't the d be lowercased? ;)
munch
@much Well, All PHP functions are case-insensitive .. so.... doesn't really matter too much.
Chacha102
@Chacha: very interesting. I guess I assumed that b/c variable names were case sensitive, functions were as well. Prob better practice to code in a case sensitive manner anyway, though
munch
+2  A: 

Perhaps you're looking for PDO::lastInsertID?

Jonathan Sampson
So .. it took me just a *little* less time to search the documentation? :P
Chacha102
I +1'd you for being first ;)
Jonathan Sampson
@much See my comment on my answer.
Chacha102