PHPGurukul

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

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year 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.
.

Recommended Tutorials for you


Add comment

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

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.