$dbo
is declared inside config.php file and it is connected to database . $id=2; // Unique id of the student record.
$step=$dbo->prepare("DELETE FROM student WHERE id=:id");
$step->bindParam(":id",$id,PDO::PARAM_INT);
$step->execute();
In the above code one record will be deleted from the table based on the id number stored in the variable $id. We can also delete more records matching to a condition. $step=$dbo->prepare("DELETE FROM student WHERE id > 10 ");
$step->execute();
All records having id more than 10 will be deleted. We can also know how many records are deleted by using rowCount() command. This command will return a numeric value saying us number of records deleted by the query just given. Here is the sample code.
$step=$dbo->prepare("DELETE FROM student WHERE id > 10 ");
$step->execute();
$no=$step->rowCount();
echo " No of records deleted = ".$no;
In this example, we use a DELETE query inside a transaction. If a condition arises where we do not want to delete the student record, we can rollback the operation to restore the data.
The DELETE query is executed to remove a student by ID.try {
$dbo->beginTransaction(); // Start the transaction
// Prepare and execute the DELETE query
$stmt = $dbo->prepare("DELETE FROM student WHERE id = ?");
$stmt->execute([5]); // Assume we are deleting the student with ID 5
// If we decide not to proceed, rollback
$dbo->rollBack();
echo "Transaction rolled back, no changes made.";
} catch (PDOException $e) {
// In case of error, rollback the transaction
$dbo->rollBack();
echo "Error: " . $e->getMessage();
}
try {
$dbo->beginTransaction();// Start the transaction
// Prepare and execute the DELETE query
$stmt = $dbo->prepare("DELETE FROM student WHERE id = ?");
$stmt->execute([5]); // Deleting student with ID 5
// Commit the transaction to make the deletion permanent
$dbo->commit();
echo "Record deleted successfully, transaction committed.";
} catch (PDOException $e) {
// Rollback in case of an error
$dbo->rollBack();
echo "Error: " . $e->getMessage();
}
$step=$dbo->prepare("DELETE FROM student where id > 10 ");
if($step->execute()){
$msg = "No of records deleted =". $step->rowCount();
}else{
$str='';
$dbo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
$a=$step->errorInfo();
while (list ($key, $val) = each ($a) ) {
$str .= "<br>$key -> $val";
}
$msg .= " Not able to delete record please contact Admin: Error Message : $str";
}
$step=$dbo->prepare("TRUNCATE student ");
//$step=$dbo->prepare("DELETE FROM student");
$step->execute();
$no=$step->rowCount();
echo " No of records deleted = ".$no;
Above code will delete all records but we won't get any output saying number of records deleted.
$step=$dbo->prepare("DELETE FROM student");
Use this line ( by removing comment from this and adding comment to TRUNCATE query ) to get 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. |
|