How can PHP be used in conjunction with a cronjob for automating tasks like a database reset?
To automate tasks like a database reset using PHP in conjunction with a cronjob, you can create a PHP script that connects to the database and executes the necessary queries to reset it. Then, you can set up a cronjob to run this PHP script at specified intervals.
<?php
// Database connection details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to reset the database
$sql = "DROP DATABASE database_name;
CREATE DATABASE database_name;
USE database_name;
-- Add your table creation queries here
// Execute the query
if ($conn->multi_query($sql) === TRUE) {
echo "Database reset successfully";
} else {
echo "Error resetting database: " . $conn->error;
}
// Close connection
$conn->close();
?>