Raphael Jeger
Raphael Jeger

Reputation: 5232

Read the last inserted row

I have the following insert statement

global $dbUser;
global $dbPW;
$query = "INSERT INTO comments (post_id, text) VALUES (:post_id,:text)";
$db = new PDO('mysql:host=localhost;dbname=test', $dbUser, $dbPW);
$statement = $db -> prepare($query);
$statement -> execute(array(':post_id' => $postId, ':text' => $text));

I want to read and return the inserted row in the same transaction.

I know I can get the

$db->lastInsertId('id')

but and I know how to do a transaction in PDO but I can't find out how to do it with a prepared statement as above (which is the preferred method for security purposes IIRC).

Thanks!

Upvotes: 0

Views: 78

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157913

You don't need no transactions here.
Just run $db->lastInsertId() if you need autogenerated id - that's all.

Upvotes: 1

Related Questions