Johan Björklund
Johan Björklund

Reputation: 774

Object of class mysqli_result could not be converted to int

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
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

And another one like that but for line 28.

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

Upvotes: 1

Views: 39434

Answers (3)

RiggsFolly
RiggsFolly

Reputation: 94672

You don't 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 FROM `info`";
    
  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 to use a variable, it can be done in pure sql.

    Replace UPDATE info SET time_left=$time_left
    With UPDATE 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
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $con = mysqli_connect($server, $username, $password, $db);

    //check time_left
    $sql = "SELECT time_left, last_sent FROM `info`";
    $result = mysqli_query($con, $sql);

    $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");
        }
    }

Upvotes: 4

wiseCoder
wiseCoder

Reputation: 1

you are comparing different data types so you have php resource at line 20 in $time_left php varible

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

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

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

Upvotes: 0

Jeroen Ingelbrecht
Jeroen Ingelbrecht

Reputation: 808

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.

if($time_left->num_rows > 0) {

Upvotes: 4

Related Questions