Reputation: 5232
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
Reputation: 157913
You don't need no transactions here.
Just run $db->lastInsertId() if you need autogenerated id - that's all.
Upvotes: 1