Cron作业执行php文件

时间:2018-03-19 03:35:08

标签: php mysql mysqli cron

我设置我的cronjob每隔一分钟运行一个php文件。 php文件访问数据库以更新一行中的值。这是php代码

<?php 

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else {
mysql_query("UPDATE tanlename SET status = 2 WHERE created < (NOW() - 
INTERVAL 1 MINUTE)");
}
mysqli_close($conn);
?>

我希望代码在发布时间1分钟后将帖子的状态设置为2。

1 个答案:

答案 0 :(得分:2)

问题出在else这里:

您需要将mysql_更改为mysqli_ - 不仅会弃用mysql_,还因为您已在mysqli_中使用$conn

然后,您需要在mysqli_query

中指定连接

更新旧代码:

else {
mysql_query("UPDATE tanlename SET status = 2 WHERE created < (NOW() - 
INTERVAL 1 MINUTE)");
}

到此:

else {
    mysqli_query($conn, "UPDATE tanlename SET status = 2 WHERE created < (NOW() - INTERVAL 1 MINUTE)");
}