Advertisement

Google Ad Slot: content-top

MySQL Delete Data

~1 min read · PHP
On this page

Deleting data in a MySQL table using PHP involves executing an SQL DELETE query. You can either use the procedural or object-oriented approach for interacting with the database.


Syntax:


DELETE FROM table_name WHERE condition;


The following example updates the name and age of the user with id=1.

Example (MySQLi Object-oriented)
connect_error) { die("Connection failed: " . $conn->connect_error); } // Delete query $sql = "DELETE FROM users WHERE id=1"; if ($conn->query($sql) === TRUE) { echo "Record deleted successfully"; } else { echo "Error deleting record: " . $conn->error; } // Close connection $conn->close(); ?>
Example (MySQLi Procedural)
Example (PDO)
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Delete query with placeholders $sql = "DELETE FROM users WHERE id=:id"; // Prepare statement $stmt = $conn->prepare($sql); // Bind parameter $stmt->bindParam(':id', $id); // Value to delete $id = 1; // Execute delete $stmt->execute(); echo "Record deleted successfully"; } catch (PDOException $e) { echo "Error deleting record: " . $e->getMessage(); } // Close connection $conn = null; ?>

Note

Without a WHERE clause, all records in the table would be deleted. Make sure to specify which row(s) you want to delete.