How can PHP be used to automatically delete expired listings in a marketplace website?

To automatically delete expired listings in a marketplace website using PHP, you can create a script that runs periodically to check the expiration date of each listing and delete any that have passed the expiration date. This can be achieved by querying the database for listings that have an expiration date earlier than the current date and then deleting those listings from the database.

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "marketplace";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query the database for listings that have expired
$current_date = date('Y-m-d H:i:s');
$sql = "DELETE FROM listings WHERE expiration_date < '$current_date'";
if ($conn->query($sql) === TRUE) {
    echo "Expired listings deleted successfully";
} else {
    echo "Error deleting expired listings: " . $conn->error;
}

// Close the database connection
$conn->close();