Saturday, November 22, 2025
HomeLanguagesPHP | MySQL Delete Query

PHP | MySQL Delete Query

The DELETE query is used to delete records from a database table.
It is generally used along with the “Select” statement to delete only those records that satisfy a specific condition.

Syntax :
The basic syntax of the Delete Query is –

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

To delete the record of the person whose ID is 201 from the ‘ Data ‘ table, the following code can be used.

Delete Query using Procedural Method :




<?php
$link = mysqli_connect("localhost", "root", "", "Mydb");
  
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
  
$sql = "DELETE FROM Data WHERE ID=201";
if(mysqli_query($link, $sql)){
    echo "Record was deleted successfully.";
else{
    echo "ERROR: Could not able to execute $sql. " 
                                   . mysqli_error($link);
}
mysqli_close($link);
?>


Output :
Table after updation –

The output on Web Browser :

Delete Query using Object Oriented Method :




<?php
$mysqli = new mysqli("localhost", "root", "", "Mydb");
   
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
  
$sql = "DELETE FROM Data WHERE ID=201";
if($mysqli->query($sql) === true){
    echo "Record was deleted successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " 
                                         . $mysqli->error;
}
  
$mysqli->close();
?>


Output :
Table After Updation –

The output on Web Browser :

Delete Query using PDO Method :




<?php
try{
    $pdo = new PDO("mysql:host=localhost;
                        dbname=Mydb", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, 
                          PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}
  
try{
    $sql = "DELETE FROM Data WHERE ID=201";
    $pdo->exec($sql);
    echo "Record was deleted successfully.";
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. "
                                . $e->getMessage());
}
unset($pdo);
?>


Output :
Table After Updation –

The output on Web Browser :

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6784 POSTS0 COMMENTS
Nicole Veronica
11929 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS