PDO

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.";
}

Recommended Tutorials for you


Related posts

PHP CRUD Operation using PDO Extension

Anuj Kumar

How to use PDO to update the database?

Anuj Kumar

How to use PDO to insert data into the database?

Anuj Kumar

PDO – PHP database extension

Anuj Kumar

How to use PDO to read data from the database?

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.