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


Tags

Anuj kumar

This is Anuj Kumar. I’m a professional web developer with 5+ years of experience. I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials. .

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Back to top button
Close
Close