PHP MySQL Delete Data

In this article, we see how to delete data from the MySQL database table by using PHP. The DELETE query is useful to delete records from a database table. It is useful along with the “Select” statement to delete only those records that satisfy a specific condition.

MySQL Delete Statement

To delete data from the database, the DELETE statement is used. We can delete data from a specific column or all columns of a table.

Read Also: PHP MySQL Where Clause

To delete selected column data from the database, the SQL query is

DELETE FROM table_name WHERE some_column=some_value;

To delete all the column data from a table, the SQL query is

DELETE FROM table_name;
or
DELETE * FROM table_name;

Example

Let us consider the table “User” with four columns ‘ ID ‘, ‘ FirstName, ‘‘ LastName, ‘and ‘ Age. ‘

MySQL User Table

Delete Query using Procedural Method

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "User";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "DELETE FROM User WHERE id=3";

if (mysqli_query($conn, $sql)) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>

Output

Table after query execution

MySQL Table After Query Execution

The output on the web browser

PHP Output on browser

Delete Query using Object-Oriented Method

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "User";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "DELETE FROM User WHERE id=3";

if ($conn->query($sql) === TRUE) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>

Output

Table after query execution

MySQL table after query execution

The output on the web browser

PHP output on browser

Delete Query using PDO Method

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "User";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "DELETE FROM User WHERE id=3";
  $conn->exec($sql);
  echo "Record deleted successfully";
  $conn->close();
} catch(PDOException $e) {
  echo $sql . "<br>" . $e->getMessage();
}

?>

Output

Table after query execution

MySQL Table after query execution

The output on the web browser

PHP Output on browser

Conclusion

The DELETE query is useful to delete records from a database table. We hope you found this article helpful and in case, you want to learn about how to connect to MySQL using PHP and creating databases, do check out our previous blogs.

Happy Learning! 🙂

Leave a Reply

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

Read previous post:
How to Install MySQL in 3 simple steps
How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer. Now, I'm using a Mac....

Close