Notice: Object of class mysqli_result could not be converted to int in database.php

Asked

Viewed 73 times

0

I’m having very specific problems while working on my crud. I created in the database a field called id, type INT and A_I and try to call it through this part of the code, but it keeps giving this error: Notice: Object of class mysqli_result could not be converted to int in C: xampp htdocs public_html inc database.php on line 25


    $database = open_database();
    $found = null;

    try {
      if ($id) {
        $sql = "SELECT * FROM " . $table . " WHERE id = " . $id;
        $result = $database->query($sql);

        if ($result > 0) {
          $found = $result->fetch_assoc();
        }

      } else {

        $sql = "SELECT * FROM " . $table;
        $result = $database->query($sql);

        if ($result > 0) {´´´


já pesquisei p caramba porém não vejo oq está errado
  • 2

    if ($result > 0), you’re comparing an object mysqli_result with an integer; this makes no sense. If you want to check if you have returned any lines, you need to use the num_rows.

  • 1

    gives a count in the $result.

1 answer

0

Change your code to the one below:

$database = open_database();
$found = null;
if ($id) {
    $sql = "SELECT * FROM " . $table . " WHERE id= " . $id;
    $result = $database->query($sql);

    if ($result->num_rows > 0) {
        $found = $result->fetch_assoc();
    }
} else {
    $sql = "SELECT * FROM " . $table;
    $result = $database->query($sql);

    if ($result->num_rows > 0) {
        $found = array();
        while ($f = $result->fetch_assoc()) {
            $found[] = $f;
    }
}

Note: The object mysqli_result is not the type INT, so I modified the code to get the num_rows who’s kind INT, with this value you can validate your block if.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.