require "config.php";// Database connection file.
$query="DELETE FROM student WHERE class='Four'";
$connection->query($query);
echo " Records Deleted ".$connection->affected_rows;
Output
Records Deleted 9
Student Table : Sample Data and query to create table
if($stmt=$connection->query("DELETE FROM student WHERE class='Four'")){
$msg.= " One class deleted <br>";
}else {
$msg.=" Class can't be deleted <br>" ;
}
With Parameters by using bind_param() <?Php
require "config.php";// Database connection
/////////////////////
$id=5;
$query="DELETE FROM student WHERE id=?";
$stmt = $connection->prepare($query);
if ($stmt) {
$stmt->bind_param('i', $id);
$stmt->execute();
echo "Record Deleted :";
echo $stmt->affected_rows;
}else{
echo $connection->error;
}
?>
$id=10;
$class='Four';
$query="DELETE FROM student WHERE id=? AND class=?";
$stmt = $connection->prepare($query);
if ($stmt) {
$stmt->bind_param('is', $id,$class);
$stmt->execute();
echo "Number of Record Deleted :". $stmt->affected_rows;
}else{
echo $connection->error;
}
Output
Number of Record Deleted :1
Procedural style
<?Php
require "config.php";// Database connection
/////////////////////
$id=6;
$class='Four';
$query="DELETE FROM student WHERE class=? AND id=?";
if ($stmt = mysqli_prepare($connection,$query)){
mysqli_stmt_bind_param($stmt, "si",$class, $id);
mysqli_stmt_execute($stmt);
echo "Number of Record Deleted :";
echo mysqli_affected_rows($connection);
}else{
echo mysqli_error($connection);
}
?>
try {
// Start transaction
$connection->begin_transaction();
// DELETE query (trying to delete a student record)
$sql = "DELETE FROM student WHERE id = 1";
if (!$connection->query($sql)) {
throw new Exception("Delete failed: " . $connection->error);
}
// Simulating an error after DELETE for rollback demonstration
if (true) { // This condition triggers rollback, replace with actual condition
throw new Exception("Simulated error, rolling back.");
}
// Commit transaction
$connection->commit();
echo "Transaction Successful. Changes committed.";
} catch (Exception $e) {
// Rollback transaction in case of an error
$connection->rollback();
echo "Transaction Failed. Rolled back. Error: " . $e->getMessage();
}
// Close connection
$connection->close();
Make sure you replace the simulated condition with an actual check based on your use case.
$sql = "DELETE FROM student WHERE id > 10";
if($connection->query($sql) === TRUE) {
$msg = "No of records deleted = " . $connection->affected_rows;
} else {
$msg = "Not able to delete record. Please contact Admin. Error Message: " . $connection->error;
}
$sql = "TRUNCATE TABLE student";
// $sql = "DELETE FROM student"; // Use this to get number of records deleted.
if($connection->query($sql) === TRUE) {
$no = $connection->affected_rows;
echo "No of records deleted = " . $no;
} else {
echo "Error: " . $connection->error;
}
The above code will delete all records but won't display the number of records deleted when using TRUNCATE.$sql = "DELETE FROM student";
You can uncomment the DELETE query and comment out the TRUNCATE query to get the number of records deleted.
| Command | Operation | Key Differences |
|---|---|---|
| DELETE | Removes rows based on a condition. |
|
| TRUNCATE | Removes all rows from the table. |
|
| DROP | Deletes the entire table structure. |
|

Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.