Fatal error: Uncaught Error: Call to Undefined method mysqli_stmt::fetch_object()

Asked

Viewed 260 times

0

code works if it is fetch() with fetch_object() of the error

public function consulta($id) {

  $mysqli = $this->conexao->getCon();
      $sql = $mysqli->prepare("SELECT * FROM pessoa WHERE id_pessoa = ?");
      $sql->bind_param("s", $id);
      $sql->execute();
      $sql->store_result();
      //asim funciona
      $sql->bind_result($id_pessoa, $nome, $nascimento, $naturalidade, $nacionalidade, $sexo, $altura, $tipo, $cpf, $rg, $alcunha, $cutis_id, $civil_id, $status_id, $pront, $carac, $att, $user_id);
      $sql->fetch();

      return $nome;

      /* assim não funciona e aparece o erro
      $obj = new Alvo();
      $obj = $sql->fetch_object();
      return $obj->nome;
      */
}

1 answer

1

In accordance with documentation, the signature of the method is mysqli_result::fetch_object. That is, it should be executed in the query result, not in the query itself.

For example:

if ($result = $mysqli->query($sql)) {
    $obj = $result->fetch_object();
    $result->close();
}

Browser other questions tagged

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