How to delete records?
1) Write the delete statement:
1 |
$sql = "DELETE FROM `users` WHERE `id`=:id"; |
2) Prepare the query:
1 |
$query = $dbh -> prepare($sql); |
3) Bind the parameters:
1 |
$query -> bindParam(':id', $id, PDO::PARAM_INT); |
4) Define the bound values:
1 |
$id = 1; |
5) Execute the query:
1 |
$query -> execute(); |
6) Check that the query has been performed and that the records have been successfully deleted from the database.
1 2 3 4 5 6 7 8 9 |
if($query -> rowCount() > 0) { $count = $query -> rowCount(); echo $count . " rows were affected."; } else { echo "No affected rows."; } |
All code together now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$sql = "DELETE FROM `users` WHERE `id`=:id"; $query = $dbh -> prepare($sql); $query -> bindParam(':id', $id, PDO::PARAM_INT); $id = 1; $query -> execute(); if($query -> rowCount() > 0) { $count = $query -> rowCount(); echo $count . " rows were affected."; } else { echo "No affected rows."; } |