MySQL

How to delete a row in MySQL using PHP

In this tutorial, we are going to see how to delete a row in MySQL using PHP. If you are a “newbie” you should know how to connect to a MySQL database before using the code below. You cannot delete a row from a table in a database if you are not connected to it.
 

 

How to delete a row in MySQL using PHP
<?php

$host = 'localhost';
$username = 'root';
$password = '';
$db = 'test';

//Create the PDO object
$pdo = new PDO("mysql:host=$host;dbname=$db", $username, $password);

//Delete a row using prepared statement
$sql = "DELETE FROM `users` WHERE `name` = :name";

//Prepare our DELETE statement
$stmt = $pdo->prepare($sql);

//Name of the user we want to remove from our 'users' table
$name = 'Alex';

//Bind the variable $name to the parameter :name
$stmt->bindValue(':name', $name);

//Execute our DELETE statement
$res = $stmt->execute();

?>
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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