我试图用我的1and1托管来设置一个cron作业。如果我写一个简单的PHP代码,cron运行正常。但是当包含数据库连接并运行代码时,它不会运行。1and1 Cron未运行mysql查询
的代码是打击:
<?php mail('[email protected]','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 protected]','Latest Blog',$latest_blog);
?>
正如你可以看到,在页面的顶部,我插一个简单的电子邮件功能,如果cron的作品只是为了测试。我每分钟都收到一封电子邮件。但是在这个简单的代码下面,我写了数据库查询并尝试用数据库中的信息发送电子邮件,但它不起作用。
有没有人有任何想法为什么?
使用'try-catch'模块来获得一个例外的电子邮件.... http://php.net/manual/en/language.exceptions.php – Hackerman
'static $ connection;'this this甚至有效? – Augwa