How can PHP and MySQL functions be effectively used together to delete specific entries from a database?
To effectively delete specific entries from a MySQL database using PHP, you can use the mysqli_query function to execute a DELETE query with a WHERE clause specifying the condition for deletion. This allows you to target and remove only the desired entries from the database.
<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Define the condition for deletion
$condition = "id = 1";
// Execute the DELETE query
$sql = "DELETE FROM table_name WHERE " . $condition;
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?>