Tutorialsplane

PHP Delete Record From Table


PHP Delete Record From Table:– The DELETE statement is used to delete records from a table.The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted.

Below example clears all the fundamentals of delete command how does it work. In addition to this we print the table after deletion of one record where id is equal to 5 that belong to William so hence one of William record is deleted from table.

Example // How to delete record from table using delete statement

<?php $servername = "localhost";
$username = "root";
$password = "";
$dbname = "employee";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn-?>connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "DELETE FROM JohnData WHERE id=5";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
	echo "<br/>";
} else {
    echo "Error deleting record: " . $conn->error;
}
$sql = "SELECT id, firstname, lastname FROM JohnData";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br/>";
    }
} else {
    echo "0 results";
}
$conn->close();

PHP Tutorial