我正在尝试为我的自动浇水创建 PHP 倒计时,我将让 crontab 每分钟运行一次并自动关闭浇水。代码如下
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include 'php_serial.class.php';
require_once 'login.php';
//connect to server
$con = mysqli_connect($server,$username,$password);
if(!$con){
die("Failed to connect:" . mysqli_connect_error());
}
else {
//check database connection
$open_db = mysqli_select_db($con,$db);
if(!$open_db){
die("Cannot connect to vatten database" . mysqli_error());
}
}
//check time_left
$sql = "SELECT time_left FROM `info`";
$time_left = mysqli_query($con,$sql);
$sql2 = "SELECT last_sent FROM `info`";
$last_sent = mysqli_query($con,$sql2);
if(!$time_left) {
die("Database access failed" . mysqli_error($con));
}
if($time_left >0) { // Error 1 here
$time_left = $time_left-1; // Error 2 here
mysqli_query($con, "UPDATE info SET time_left=$time_left");
}
elseif($time_left <1 && $last_sent !== "[LOOOOOO]") {
$serial = new phpSerial;
$serial->deviceSet("/dev/ttyUSB0");
$serial->confBaudRate(1200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
$serial->sendMessage("[LOOOOOO]");
mysqli_query($con, "UPDATE info SET time_left=$time_left");
}
mysqli_close($con);
?>
所以我得到的错误是
注意:类 mysqli_result 的对象无法在第 27 行的 /var/www/vatten/check.php 中转换为 int
还有一个类似的,但在第 28 行。
除此之外,它将数据库中的“time_left”重置为0