Is there a WHERE-NOT condition in MySQL delete commands in PHP?
There is no WHERE-NOT condition in MySQL delete commands in PHP. However, you can achieve the same result by using the NOT IN clause in the WHERE condition. This allows you to specify a list of values that should not be included in the deletion process.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Define the list of values to exclude
$excludeList = [1, 2, 3];
// Execute the delete command with NOT IN clause
$sql = "DELETE FROM table_name WHERE column_name NOT IN (" . implode(",", $excludeList) . ")";
if ($conn->query($sql) === TRUE) {
echo "Records deleted successfully";
} else {
echo "Error deleting records: " . $conn->error;
}
// Close the connection
$conn->close();
?>