Hello,
Recently I’ve add a “Crob Job” using “Cron Jobs” tool in “CPanel”.
The Cron Job executes command below once per minute (* * * * *):
php /home/melkobi/public_html/index.php
In the mentioned “index.php” source code, I’ve used the query below to insert a new row to the table1:
$query = “INSERT INTO table1 (temp) VALUES (’$temp’)”;
Good part: Whenever I visit my website using browser (Firefox) and refresh the tab, a new row including temperature ('C) will be added to table (showing everything including query and connection works perfectly).
The problem is whenever I close the browser and rely on Cron Job itself to execute “index.php”, wrong value “false” will be added to table instead of temperature (showing the query will be send, but “false” will be added to table instead of temperature).
---------- Questions:
1 - Is the problem with “Cron Job” or with the “query”?
2 - Is the problem related to sql comand? I’ve used commands below:
// Creating connection:
$conn = mysqli_connect('myServerName', 'myUserName', 'myPassword', 'myDBname);
// Checking connection:
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Add temperature as a new row:
$query = "INSERT INTO table1 (temp) VALUES ('$temp')";
// Checking for error:
if (mysqli_multi_query($conn, $query)) {
echo "New records created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($conn);
}
// Closing the connection:
mysqli_close($conn);
3- Is the problem related to PHP version? (My virtual web host PHP version: v7.2)
4- Is the problem with the string “$query”? (Did I concatenate string and variable perfectly?)
I’ve googled all day long and nothing practical found. I’ll be appreciated if any suggestion.
Regards