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