php mysqli_result 类的对象无法转换为 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18431450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:33:19  来源:igfitidea点击:

Object of class mysqli_result could not be converted to int

phpmysql

提问by Johan Bj?rklund

i'm trying to create a php countdown for my automated watering, i'm going to have crontab run this every minute and turn of the watering automatically. the code is as follows

我正在尝试为我的自动浇水创建一个 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);
?>

So the error i'm getting is Notice: Object of class mysqli_result could not be converted to int in /var/www/vatten/check.php on line 27

所以我得到的错误是注意:类 mysqli_result 的对象无法在第 27 行的 /var/www/vatten/check.php 中转换为 int

and another one like that but for line 28.

和另一个类似的,但对于第 28 行。

And apart from that it resets the "time_left" in the database to 0

除此之外,它将数据库中的“time_left”重置为 0

Any help would be appreciated!

任何帮助,将不胜感激!

回答by RiggsFolly

You dont appear to understand what you are doing

你似乎不明白你在做什么

  1. You can return more than one column from a table with a single query.

    $sql = "SELECT time_left, last_sent FROMinfo";

  2. Once the query has been run, you then need to collect the results one row at a time using mysqli_fetch_? to return either an array or an object containing the requested column values.

    $row = mysqli_fetch_assoc($result);

  3. This query does not need tou use a variable, it can be dow in pure sql.

    Replace UPDATE info SET time_left=$time_leftWith UPDATE info SET time_left=time_left-1

  1. 您可以使用单个查询从表中返回多个列。

    $sql = "SELECT time_left, last_sent FROM信息";

  2. 运行查询后,您需要使用 mysqli_fetch_ 一次收集一行结果?返回包含请求的列值的数组或对象。

    $row = mysqli_fetch_assoc($result);

  3. 这个查询不需要使用变量,可以在纯sql中使用。

    更换UPDATE info SET time_left=$time_leftUPDATE info SET time_left=time_left-1

So does this seem a more organized structure for your code.

对于您的代码来说,这似乎是一个更有条理的结构。

<?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());
    } 
    //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, last_sent FROM `info`";
    $result = mysqli_query($con,$sql);
    if ( ! $result ) {
        die('Query failed ' . $sql . ' Error:' . mysqli_error());
    }

    $row = mysqli_fetch_assoc($result);

    if( $row['time_left'] > 0 ) {
        mysqli_query($con, "UPDATE info SET time_left=time_left-1");
    } else {
         if($row['time_left'] < 1 && $row['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]");
            // does not seem to achieve anything
            // mysqli_query($con, "UPDATE info SET time_left=$time_left");  
        }
    }
mysqli_close($con);
?>

回答by Jeroen Ingelbrecht

The statement

该声明

$time_left = mysqli_query($con,$sql);

returns an object of type mysqli_result. Use the num_rows property of the result. That's an integer.

返回一个 mysqli_result 类型的对象。使用结果的 num_rows 属性。那是一个整数。

if($time_left->num_rows > 0) {

回答by wiseCoder

you are comparing different data typesso you have php resource at line 20in $time_leftphp varible

您正在比较不同的数据类型,因此您在php 变量的第 20 行拥有 php 资源$time_left

$time_left = mysqli_query($con,$sql);//line 20

and you are comparing $time_left(php resource) with int 0 at line 27

并且您在第 27 行将$time_left(php 资源) 与int 0进行比较

if($time_left >0) { // Error 1 here //line 27