How can a cron job be set up to regularly update a MySQL database with new data in PHP?
To regularly update a MySQL database with new data using a cron job in PHP, you can create a PHP script that connects to the database, retrieves the new data, and updates the database accordingly. You can then set up a cron job to run this script at specified intervals.
<?php
// Connect to MySQL 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);
}
// Retrieve new data
$newData = "new data to be updated";
// Update database with new data
$sql = "UPDATE table_name SET column_name = '$newData' WHERE condition";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>