Reputation: 303
I updated a row in database using PDO.
$stmt = $db->prepare("UPDATE product SET price=? WHERE seller=?");
$stmt->execute(array(456,"Apple"));
This didn't worked for me
$updated_id = $db->lastInsertId();
How can i get the id of that updated row.
Upvotes: 5
Views: 13135
Reputation: 1
Use this
$sql="Update table_1 set col_1=?,col_id = LAST_INSERT_ID(col_id) where col_2=? limit 1;SELECT LAST_INSERT_ID() last_update_id;";
$stmt =$dbh->prepare($sql);
$stmt->execute(array('new_val','cond_val'));
$stmt->nextRowset();
$rez=$stmt->fetch(PDO::FETCH_ASSOC);
print_r($rez);
Upvotes: -1
Reputation: 3682
Please have a look at this answer How to get ID of the last updated row in MySQL?
or do a select on same where clause (If you think it will have only one record)
$stmt = $db->query("SELECT id FROM product WHERE seller=:seller");
$stmt->bindParam(':seller', $sellerName, PDO::PARAM_STR)
$row =$stmt->fetchObject();
echo $row->id;
Upvotes: 0
Reputation: 2775
Best you can do is use PDO::rowCount()
to get the number of rows affected. Remember, your update statement may not update only one, so, there is no way to just get a single id.
Upvotes: 3