How to resolve a "Catchable fatal error" error

Asked

Viewed 4,635 times

5

I’m doing some object orientation exercises in PHP and when changing the database data, I come across the following error

Catchable fatal error: Object of class mysqli_result could not be converted to string in /var/www/html/Student/class/Alunodao.php on line 30

I’ve never seen him before (in my long career of a few months). Can anyone tell me what it means?

Just follow my code

Altera-data.php

$id = $_POST['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$senha = $_POST['senha'];

$alunoDAO = new AlunoDAO();

$nome_imagem = $alunoDAO->buscaFoto($conexao, $id);

$aluno = new Aluno($nome, $cpf, $senha, $nome_imagem);

$alunoDAO->alteraDados($conexao, $aluno, $id);

and my function in the class AlunoDAO (the one being pointed out an error).

function alteraDados($conexao, $aluno, $id){
        $senhaMD5 = md5($aluno->getSenha());
        $query = "update alunos set nome = '{$aluno->getNome()}', cpf = '{$aluno->getCpf()}', senha = '{$senhaMD5}', imagem = '{$aluno->getNomeImagem()}' where id = {$id}";
        $resultado = mysqli_query($conexao, $query);
        return $resultado;
    }
  • Which line is the error?

  • on the $query line

  • Some of the values passed are object and not a string, print the query.

  • What do you mean, rray?

  • ago echo $query;

  • All the right values appear?

  • ah, I had done it and it does not return me anything. I went to the page where the error appears, I went to the class link and nothing.

  • Look, I’m gonna post an answer, put it in your script, and then say what it came back to.

Show 4 more comments

1 answer

5


To avoid errors of the type Catchable error, should capture the exception thrown, to obtain more detailed and controlled information of the error, and exceptions sometimes make available some sensitive data.

The solution, is to instantiate this function, and call it on the page where the errors are returned, in your case, will be on the page where you create an instance of the classes.

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if ( E_RECOVERABLE_ERROR===$errno ) {

        echo "'Catched:' catchable fatal error (". $errstr .")\n";

        return true;    
    }
    return false;
}

set_error_handler('myErrorHandler');

To correct the error, in your job, simply do the fetch, before trying to return the value:

function buscaFoto($conexao, $id){
        $query = "select imagem from alunos where id = {$id}";
        $resultado = mysqli_query($conexao, $query);
        if($resultado){
            $linha = mysqli_fetch_assoc($resultado);
            return $linha['imagem'];    
        } else {
            return false;   
        }
    }

For more information read:

Set_error_handler - PHP.net

Catching Catchable Errors - Soen

  • That’s exactly what it was... Thank you very much!

  • Look, take advantage and save the part that captures the exceptions. It is usually useful to create logs.

  • All right, tip noted!

Browser other questions tagged

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