I am trying to set up a cron job with my 1and1 hosting. If i write a simple php code, cron runs normal. But when include the database connection and run the code, it does not run.
The code is blow:
<?php mail('email@gmail.com','At4U test cron','testing cron'); ?>
<?php
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/AT4U/extensions/phpmailer/class.phpmailer.php');
include(__ROOT__.'/AT4U/core/functions/general.php');
function db_connect() {
$db_name = '***************';
$db_user = '***************';
$host = '***************';
$password = '***************';
// Define connection as a static variable, to avoid connecting more than once
static $connection;
// Try and connect to the database, if a connection has not been established yet
if(!isset($connection)) {
$connection = mysqli_connect($host,$db_user,$password,$db_name);
}
// If connection was not successful, handle the error
if($connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return mysqli_connect_error();
}
return $connection;
}
$con = db_connect();
$sql = "SELECT * FROM blog WHERE active = 1 ORDER BY id DESC LIMIT 1";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$latest_blog = $row['title'];
}
}
email('email@gmail.com','Latest Blog',$latest_blog);
?>
As you can see, at the top of the page, i inserted a simple email function just to test if the cron works. I get the email every minute. But below this simple code, i wrote the database query and try sending email with the information from database, but it does not work.
Does anyone have any idea why?